我有一个方法,它按如下所述读取文件,并将每个名字,姓氏直到指定大小的条目添加到列表中并返回列表。
该文件看起来像这样:
Smith
John
newline
Jeff
Johnny
newline
我们有名字,姓氏,空行,然后是文本文件中最后一行的名称的下一个条目,也是空行。因此,如果我们将大小设置为1,那么只有第一个条目,因此,[Smith, John]
应该添加到列表中然后返回。如果大小为2则应返回两个条目,因此[['Smith', 'John'], ['jeff', 'johnny']]
我的尝试:
def addToList(file, size):
newList = []
temp = []
with open(file, "r") as f:
# read each line in the file
for line in f:
# if the len of the newlist is less then the size then check
# to see if its either a new line or not.
if len(newList) < size:
# if its not a new line than add the line to a temp list
if line != "\n":
temp.append(line.strip())
# if it is then that means we're going to move on
# to a new entry so add this entry to the newList
# and reset the temp list.
else:
newList.append(temp)
temp = []
return newList
所以我运行addToList("random.txt", 1)
我得到[['Smith', 'John']]
这是正确的结果,因为size = 1 =一个条目。但是,我所拥有的问题是,如果我将大小设置为2,那么如果我addToList("random.txt", 2)
我只获得名称的第一个条目而不是2个条目的列表。所以我得到的是:[['Smith', 'John']]
而不是[['Smith', 'John'], ['jeff', 'johnny']]
似乎它没有读取最后一个空行,但我该如何解决这个问题呢?如果有人能帮助我,那将非常感激。
澄清的例子:
Lets say text file is this:
Smith
John
emptyline
John
Smith
emptyline
Smith
John
emptyline
John
Jeff
emptyline
results should be:
addToList("random.txt", 1) -> [['Smith', 'John']] because size = 1 = # of entry = 1
addToList("random.txt", 2) -> [['Smith', 'John'], ['John', 'Smith']]
addToList("random.txt", 3) -> [['Smith', 'John'], ['John', 'Smith'], ['Smith', 'John']]
addToList("random.txt", 4) -> [['Smith', 'John'], ['John', 'Smith'], ['Smith', 'John'], ['John', 'Jeff']]
答案 0 :(得分:1)
你可以这样做,
with open('file') as f:
fil = f.read()
print([i.split('\n') for i in fil.split('\n\n') if i])
输出:
[['Smith', 'John'], ['Jeff', 'Johnny']]
答案 1 :(得分:0)
简单的方法是构建一个累加器来抓取每个非空白行,并在遇到空行时将其附加到结果中。
def read_file(path):
with open(path) as inf:
result = []
group = []
for line in inf:
line = line.strip()
if not line: # blank line
result.append(group)
group = []
else:
group.append(line)
return result
另一种选择是:
def read_file(path, groupsize):
with open(path) as inf:
while True:
try:
yield [next(inf).strip() for _ in range(groupsize)]
except StopIteration:
# EOF
return
next(inf) # skip the blank line