짝수 및 홀수 길이 파일에 대해 작동합니다. 일치하지 않는 마지막 줄을 무시합니다.
f=file("file")
lines = f.readlines()
for even, odd in zip(lines[0::2], lines[1::2]):
print "even : ", even
print "odd : ", odd
print "end cycle"
f.close()
대용량 파일이있는 경우 올바른 방법이 아닙니다. readlines ()를 사용하여 메모리의 모든 파일을로드합니다. 한 번은 각 줄 시작의 fseek 위치를 저장하는 파일을 읽는 클래스를 작성했습니다. 이를 통해 모든 파일을 메모리에 저장하지 않고도 특정 줄을 가져올 수 있으며 앞뒤로 이동할 수도 있습니다.
여기에 붙여 넣습니다. 라이센스는 퍼블릭 도메인입니다. 즉, 원하는 것을 수행하십시오. 이 수업은 6 년 전에 작성되었으며 그 이후로 만지거나 확인하지 않았습니다. 나는 그것이 파일을 준수하지 않는다고 생각합니다. 주의 사항 . 또한 이것은 귀하의 문제에 대한 과잉입니다. 나는 당신이 확실히이 방법으로 가야한다고 주장하는 것은 아니지만, 나는이 코드를 가지고 있었고 더 복잡한 액세스가 필요한 경우 공유하는 것을 즐깁니다.
import string
import re
class FileReader:
"""
Similar to file class, but allows to access smoothly the lines
as when using readlines(), with no memory payload, going back and forth,
finding regexps and so on.
"""
def __init__(self,filename):
self.__file=file(filename,"r")
self.__currentPos=-1
self.__file.seek(0,0)
counter=0
line=self.__file.readline()
while line != '':
counter = counter + 1
line=self.__file.readline()
self.__length = counter
self.__file.seek(0,0)
self.__lineToFseek = []
while True:
cur=self.__file.tell()
line=self.__file.readline()
self.__lineToFseek.append(cur)
if line == '':
break
def __len__(self):
"""
member function for the operator len()
returns the file length
FIXME: better get it once when opening file
"""
return self.__length
def __getitem__(self,key):
"""
gives the "key" line. The syntax is
import FileReader
f=FileReader.FileReader("a_file")
line=f[2]
to get the second line from the file. The internal
pointer is set to the key line
"""
mylen = self.__len__()
if key < 0:
self.__currentPos = -1
return ''
elif key > mylen:
self.__currentPos = mylen
return ''
self.__file.seek(self.__lineToFseek[key],0)
counter=0
line = self.__file.readline()
self.__currentPos = key
return line
def next(self):
if self.isAtEOF():
raise StopIteration
return self.readline()
def __iter__(self):
return self
def readline(self):
"""
read a line forward from the current cursor position.
returns the line or an empty string when at EOF
"""
return self.__getitem__(self.__currentPos+1)
def readbackline(self):
"""
read a line backward from the current cursor position.
returns the line or an empty string when at Beginning of
file.
"""
return self.__getitem__(self.__currentPos-1)
def currentLine(self):
"""
gives the line at the current cursor position
"""
return self.__getitem__(self.__currentPos)
def currentPos(self):
"""
return the current position (line) in the file
or -1 if the cursor is at the beginning of the file
or len(self) if it's at the end of file
"""
return self.__currentPos
def toBOF(self):
"""
go to beginning of file
"""
self.__getitem__(-1)
def toEOF(self):
"""
go to end of file
"""
self.__getitem__(self.__len__())
def toPos(self,key):
"""
go to the specified line
"""
self.__getitem__(key)
def isAtEOF(self):
return self.__currentPos == self.__len__()
def isAtBOF(self):
return self.__currentPos == -1
def isAtPos(self,key):
return self.__currentPos == key
def findString(self, thestring, count=1, backward=0):
"""
find the count occurrence of the string str in the file
and return the line catched. The internal cursor is placed
at the same line.
backward is the searching flow.
For example, to search for the first occurrence of "hello
starting from the beginning of the file do:
import FileReader
f=FileReader.FileReader("a_file")
f.toBOF()
f.findString("hello",1,0)
To search the second occurrence string from the end of the
file in backward movement do:
f.toEOF()
f.findString("hello",2,1)
to search the first occurrence from a given (or current) position
say line 150, going forward in the file
f.toPos(150)
f.findString("hello",1,0)
return the string where the occurrence is found, or an empty string
if nothing is found. The internal counter is placed at the corresponding
line number, if the string was found. In other case, it's set at BOF
if the search was backward, and at EOF if the search was forward.
NB: the current line is never evaluated. This is a feature, since
we can so traverse occurrences with a
line=f.findString("hello")
while line == '':
line.findString("hello")
instead of playing with a readline every time to skip the current
line.
"""
internalcounter=1
if count < 1:
count = 1
while 1:
if backward == 0:
line=self.readline()
else:
line=self.readbackline()
if line == '':
return ''
if string.find(line,thestring) != -1 :
if count == internalcounter:
return line
else:
internalcounter = internalcounter + 1
def findRegexp(self, theregexp, count=1, backward=0):
"""
find the count occurrence of the regexp in the file
and return the line catched. The internal cursor is placed
at the same line.
backward is the searching flow.
You need to pass a regexp string as theregexp.
returns a tuple. The fist element is the matched line. The subsequent elements
contains the matched groups, if any.
If no match returns None
"""
rx=re.compile(theregexp)
internalcounter=1
if count < 1:
count = 1
while 1:
if backward == 0:
line=self.readline()
else:
line=self.readbackline()
if line == '':
return None
m=rx.search(line)
if m != None :
if count == internalcounter:
return (line,)+m.groups()
else:
internalcounter = internalcounter + 1
def skipLines(self,key):
"""
skip a given number of lines. Key can be negative to skip
backward. Return the last line read.
Please note that skipLines(1) is equivalent to readline()
skipLines(-1) is equivalent to readbackline() and skipLines(0)
is equivalent to currentLine()
"""
return self.__getitem__(self.__currentPos+key)
def occurrences(self,thestring,backward=0):
"""
count how many occurrences of str are found from the current
position (current line excluded... see skipLines()) to the
begin (or end) of file.
returns a list of positions where each occurrence is found,
in the same order found reading the file.
Leaves unaltered the cursor position.
"""
curpos=self.currentPos()
list = []
line = self.findString(thestring,1,backward)
while line != '':
list.append(self.currentPos())
line = self.findString(thestring,1,backward)
self.toPos(curpos)
return list
def close(self):
self.__file.close()