2012-05-01 9 views
5

मैं एक छोटा प्रोग्राम बना रहा हूं जो किसी दस्तावेज़ से पाठ को पढ़ और प्रदर्शित करेगा। मुझे एक टेस्ट फाइल मिली है जो इस तरह दिखती है:क्या कोई .txt फ़ाइल पढ़ने और स्मृति को प्रत्येक पंक्ति को स्टोर करने का कोई तरीका है?

12,12,12 
12,31,12 
1,5,3 
... 

और इसी तरह। अब मैं प्रत्येक पंक्ति पढ़ सकते हैं और स्मृति में संग्रहीत करने के अजगर चाहते हैं, इसलिए जब आप डेटा को प्रदर्शित करने का चयन करें, यह इस तरह के रूप में खोल प्रदर्शित करेगा:

1. 12,12,12 
2. 12,31,12 
... 

और इतने पर। मैं यह कैसे कर सकता हूँ?

+8

हमें अपने कोड दिखाएं अब तक – Jordonias

+3

'pydoc file.readlines' – bjarneh

+1

@bjarneh readlines() स्मृति कुशल नहीं है। –

उत्तर

13

मैं यह पता पहले से ही उत्तर है :) ऊपर संक्षेप में:

# It is a good idea to store the filename into a variable. 
# The variable can later become a function argument when the 
# code is converted to a function body. 
filename = 'data.txt' 

# Using the newer with construct to close the file automatically. 
with open(filename) as f: 
    data = f.readlines() 

# Or using the older approach and closing the filea explicitly. 
# Here the data is re-read again, do not use both ;) 
f = open(filename) 
data = f.readlines() 
f.close() 


# The data is of the list type. The Python list type is actually 
# a dynamic array. The lines contain also the \n; hence the .rstrip() 
for n, line in enumerate(data, 1): 
    print '{:2}.'.format(n), line.rstrip() 

print '-----------------' 

# You can later iterate through the list for other purpose, for 
# example to read them via the csv.reader. 
import csv 

reader = csv.reader(data) 
for row in reader: 
    print row 

यह मेरी कंसोल पर प्रिंट:

1. 12,12,12 
2. 12,31,12 
3. 1,5,3 
----------------- 
['12', '12', '12'] 
['12', '31', '12'] 
['1', '5', '3'] 
4

एक सरणी

f = open("file.txt", "r") 
a = [] 
for line in f: 
    a.append(line) 
+0

इसे करने का बहुत आसान तरीका। !! – Surya

+5

को 'ए = ओपन ("file.txt") के रूप में भी जाना जाता है। रीडलाइन() ', या अधिक समतुल्य' ए = सूची (खुला (" file.txt "))'। फ़ाइल को बंद करने के लिए आपको वास्तव में 'साथ' कथन का उपयोग करना चाहिए; यह ऐसा करने के लिए सीपीथन रेफ-गिनती सेमेन्टिक्स पर निर्भर करता है और उदाहरण के तहत अपेक्षित के रूप में काम नहीं करेगा PyPy। – Dougal

+0

यह समस्या में नहीं चलता है अगर फ़ाइल फ़ाइल में अधिक संख्या में लाइनों के साथ बहुत बड़ा है ?? – Surya

1

में भंडारण की कोशिश तुम भी csv मॉड्यूल में दिलचस्पी हो सकती है। यह आप पार्स, पढ़ सकते हैं और अल्पविराम विभाजित मान में फाइल (CSV) स्वरूप है ... जो अपने उदाहरण में प्रतीत होता है में लिखने की सुविधा देता है

उदाहरण:।

import csv 
reader = csv.reader(open('file.txt', 'rb'), delimiter=',') 
#Iterate over each row 
for idx,row in enumerate(reader): 
    print "%s: %s"%(idx+1,row) 
0
with open('test.txt') as o: 
    for i,t in enumerate(o.readlines(), 1): 
     print ("%s. %s"% (i, t)) 
0
#!/usr/local/bin/python 

t=1 

with open('sample.txt') as inf: 
    for line in inf: 
     num = line.strip() # contains current line 
     if num: 
      fn = '%d.txt' %t # gives the name to files t= 1.txt,2.txt,3.txt ..... 
      print('%d.txt Files splitted' %t) 
      #fn = '%s.txt' %num 
      with open(fn, 'w') as outf: 
       outf.write('%s\n' %num) # writes current line in opened fn file 
       t=t+1 
1

@PePr उत्कृष्ट समाधान के लिए धन्यवाद। इसके अतिरिक्त, आप अंतर्निहित विधि String.join(data) के साथ .txt फ़ाइल को मुद्रित करने का प्रयास कर सकते हैं। उदाहरण के लिए:

with open(filename) as f: 
    data = f.readlines() 
print(''.join(data)) 
संबंधित मुद्दे

 संबंधित मुद्दे