2012-02-11 12 views
8

मैं आमतौर पर निम्नलिखित पायथन कोड का उपयोग एक फ़ाइल से लाइनों को पढ़ने के लिए:पायथन - एनयूएल सीमांकित लाइनों के साथ फाइल कैसे पढ़ा जाए?

f = open('./my.csv', 'r') 
for line in f: 
    print line 

लेकिन यह कैसे फ़ाइल से "\ 0" (नहीं "\ n") सीमांकित लाइन है, तो कैसा रहेगा? क्या कोई पाइथन मॉड्यूल है जो इसे संभाल सकता है?

किसी भी सलाह के लिए धन्यवाद।

उत्तर

13

यदि आपकी फ़ाइल स्मृति में काफी छोटा है कि आप यह सब पढ़ सकते हैं आप विभाजन का उपयोग कर सकते हैं:

def fileLineIter(inputFile, 
       inputNewline="\n", 
       outputNewline=None, 
       readSize=8192): 
    """Like the normal file iter but you can set what string indicates newline. 

    The newline string can be arbitrarily long; it need not be restricted to a 
    single character. You can also set the read size and control whether or not 
    the newline string is left on the end of the iterated lines. Setting 
    newline to '\0' is particularly good for use with an input file created with 
    something like "os.popen('find -print0')". 
    """ 
    if outputNewline is None: outputNewline = inputNewline 
    partialLine = '' 
    while True: 
     charsJustRead = inputFile.read(readSize) 
     if not charsJustRead: break 
     partialLine += charsJustRead 
     lines = partialLine.split(inputNewline) 
     partialLine = lines.pop() 
     for line in lines: yield line + outputNewline 
    if partialLine: yield partialLine 
:

for line in f.read().split('\0'): 
    print line 

नहीं तो आप इस feature request के बारे में चर्चा से यह नुस्खा की कोशिश करना चाहते हो सकता है


मैंने यह भी देखा कि आपकी फ़ाइल में "सीएसवी" एक्सटेंशन है। पाइथन (आयात सीएसवी) में निर्मित एक सीएसवी मॉड्यूल है।

Dialect.lineterminator

लेखक द्वारा उत्पादित लाइनों को समाप्त करने के लिए प्रयोग किया जाता स्ट्रिंग: वहाँ एक विशेषता Dialect.lineterminator लेकिन यह वर्तमान में रीडर में लागू नहीं है कहा जाता है। यह '\ r \ n' पर डिफ़ॉल्ट है।

नोट पाठक को 'आर' या '\ n' को अंत-रेखा के रूप में पहचानने के लिए हार्ड-कोड किया गया है, और लिनेटरमिनेटर को अनदेखा करता है। भविष्य में यह व्यवहार बदल सकता है।

+0

मेरी फाइलें कई हजारों हजारों लाइनों में कई हजार होंगी। – user1129812

+0

@ user1129812: एक लाइन कब तक है? 100 बाइट्स? 100 बाइट्स * 50000 लाइनें = लगभग। 5 एमबी –

+0

प्रत्येक पंक्ति लगभग 100 अक्षर होना चाहिए। यूनिकोड मानें, प्रत्येक पंक्ति 200 बाइट्स होगी, और 50000 लाइनों की फाइल के बारे में 200 x 50000 = 9.54 एमबी होगा। – user1129812

0

मैंने मार्क बेयर्स के सुझाव को संशोधित किया है ताकि हम पाइथन में एनयूएल सीमांकित लाइनों के साथ फ़ाइल को रीडलाइन कर सकें। यह दृष्टिकोण लाइन द्वारा संभावित रूप से बड़ी फ़ाइल लाइन को पढ़ता है और अधिक मेमोरी कुशल होना चाहिए। यहां पायथन कोड (टिप्पणियों के साथ) है:

import sys 

# Variables for "fileReadLine()" 
inputFile = sys.stdin # The input file. Use "stdin" as an example for receiving data from pipe. 
lines = [] # Extracted complete lines (delimited with "inputNewline"). 
partialLine = '' # Extracted last non-complete partial line. 
inputNewline="\0" # Newline character(s) in input file. 
outputNewline="\n" # Newline character(s) in output lines. 
readSize=8192 # Size of read buffer. 
# End - Variables for "fileReadLine()" 

# This function reads NUL delimited lines sequentially and is memory efficient. 
def fileReadLine(): 
    """Like the normal file readline but you can set what string indicates newline. 

    The newline string can be arbitrarily long; it need not be restricted to a 
    single character. You can also set the read size and control whether or not 
    the newline string is left on the end of the read lines. Setting 
    newline to '\0' is particularly good for use with an input file created with 
    something like "os.popen('find -print0')". 
    """ 
    # Declare that we want to use these related global variables. 
    global inputFile, partialLine, lines, inputNewline, outputNewline, readSize 
    if lines: 
     # If there is already extracted complete lines, pop 1st llne from lines and return that line + outputNewline. 
     line = lines.pop(0) 
     return line + outputNewline 
    # If there is NO already extracted complete lines, try to read more from input file. 
    while True: # Here "lines" must be an empty list. 
     charsJustRead = inputFile.read(readSize) # The read buffer size, "readSize", could be changed as you like. 
     if not charsJustRead: 
      # Have reached EOF. 
      if partialLine: 
      # If partialLine is not empty here, treat it as a complete line and copy and return it. 
      popedPartialLine = partialLine 
      partialLine = "" # partialLine is now copied for return, reset it to an empty string to indicate that there is no more partialLine to return in later "fileReadLine" attempt. 
      return popedPartialLine # This should be the last line of input file. 
      else: 
      # If reached EOF and partialLine is empty, then all the lines in input file must have been read. Return None to indicate this. 
      return None 
     partialLine += charsJustRead # If read buffer is not empty, add it to partialLine. 
     lines = partialLine.split(inputNewline) # Split partialLine to get some complete lines. 
     partialLine = lines.pop() # The last item of lines may not be a complete line, move it to partialLine. 
     if not lines: 
      # Empty "lines" means that we must NOT have finished read any complete line. So continue. 
      continue 
     else: 
      # We must have finished read at least 1 complete llne. So pop 1st llne from lines and return that line + outputNewline (exit while loop). 
      line = lines.pop(0) 
      return line + outputNewline 


# As an example, read NUL delimited lines from "stdin" and print them out (using "\n" to delimit output lines). 
while True: 
    line = fileReadLine() 
    if line is None: break 
    sys.stdout.write(line) # "write" does not include "\n". 
    sys.stdout.flush() 

उम्मीद है कि यह मदद करता है।

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