String IO का उपयोग
StringIO आपको किसी string को file की तरह treat करने देता है, जो demos, tests और API responses के लिए आदर्श है।
In this page:
Syntax
from io import StringIO
df = pd.read_csv(StringIO(text))
String IO
io.StringIO text को एक file-like object में wrap करता है जिसे Pandas पढ़ या लिख सकता है। यह disk को छूने से बचाता है। string पाने के लिए to_csv को बिना path के call करें, या output capture करने के लिए StringIO buffer पास करें।
Note:
अभी-अभी लिखे गए buffer को फिर से पढ़ने से पहले buf.seek(0) call करें।
उदाहरण: String IO
import io
import pandas as pd
df = pd.DataFrame({"a": [1, 2], "b": ["x", "y"]})
buf = io.StringIO()
df.to_csv(buf, index=False)
buf.seek(0)
print(buf.read())
buf.seek(0)
print(pd.read_csv(buf).equals(df))
# Output:
# a,b
# 1,x
# 2,y
#
# True
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- लिखने के बाद seek(0) भूल जाना
- नए read_json और read_html को plain string पास करना
- exhausted buffer का फिर से उपयोग करना
चैप्टर सारांश
- StringIO strings को file-like बनाता है
- Disk की ज़रूरत नहीं
- seek(0) rewind करता है
- tests और demos के लिए बढ़िया
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: