← Back to Pandas Course | Chapter 11: File I/O | Lesson 7 of 7

String IO का उपयोग

StringIO आपको किसी string को file की तरह treat करने देता है, जो demos, tests और API responses के लिए आदर्श है।

In this page:

  1. String IO
Syntax
python
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

python
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. #}
आम गलतियां
  1. लिखने के बाद seek(0) भूल जाना
  2. नए read_json और read_html को plain string पास करना
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.