← Back to NumPy Course | Chapter 11: File I/O | Lesson 2 of 5

np.savetxt/loadtxt का इस्तेमाल

savetxt plain-text files लिखता है जिन्हें आप कहीं भी खोल सकते हैं और loadtxt इन्हें वापस पढ़ता है।

In this page:

  1. np.savetxt/loadtxt
Syntax
python
np.savetxt("file.txt", arr, delimiter=",")
arr = np.loadtxt("file.txt", delimiter=",")

np.savetxt/loadtxt

np.savetxt एक array को delimited text की तरह लिखता है और np.loadtxt इसे एक array में पढ़ता है। CSV-like files के लिए delimiter="," और number formatting control करने के लिए fmt इस्तेमाल करें। Text files portable हैं लेकिन बड़ी हैं और exact dtype information खो देती हैं।

Note: header और comments arguments आपको एक title line add और skip करने देते हैं।

उदाहरण: np.savetxt/loadtxt

python
import numpy as np
import io

a = np.array([[1.5, 2.5], [3.5, 4.5]])
buf = io.StringIO()
np.savetxt(buf, a, delimiter=",", fmt="%.1f")
text = buf.getvalue()
print(text)
back = np.loadtxt(io.StringIO(text), delimiter=",")
print(back)

# Output:
# 1.5,2.5
# 3.5,4.5
#
# [[1.5 2.5]
#  [3.5 4.5]]
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. पढ़ते वक्त delimiter भूल जाना
  2. एक short fmt से precision खोना
  3. loadtxt इस्तेमाल करते हुए missing values वाला text load करने की कोशिश करना
चैप्टर सारांश
  • savetxt text लिखता है
  • loadtxt text पढ़ता है
  • delimiter separator set करता है
  • fmt formatting control करता है
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 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.