python split()不处理整个文本
我读了一个文本文件,上面有几个块(运行)被“ \ n \ n'隔开,并且正在寻找第一行中有某个字符串的块。我的代码:
runinfo=""
with open("./Project/Clean/indexfile.txt") as file:
text=file.read()
runs = text.split('\n\n') #split text into blocks for different runs
for i in range(num_files):
runinfotxt="There is no information to run_id "+Run_ids[i]+" in the indexfile"
for block in runs: #search block with corresponding run_id
firstline=""
for let in block:
firstline=firstline+let
if let=="\n":
break
print("FIRSTLINE: ",firstline)
if firstline ==("run_id: "+Run_ids[i]+"\n"):
runinfotxt=block
runinfo=runinfo+block
else:
pass
问题:不是处理完整的文件。 “文本”包括整个文件。但是,当打印列表“运行”的长度时,它会返回74,尽管我的文件有更多的块。
如果在前74个块中找到正确的块,则可以很好地工作。我遇到某种尺寸限制吗?我没有错误消息。它的举止好像文件要短得多。
编辑:“运行”中的项目数取决于所读取的文件。我在文件中添加了一些(约15个)块,现在“运行”的长度为94。
I read a text file with several blocks (runs) separated by '\n\n' and am looking for the block with a certain string in the first line. My code:
runinfo=""
with open("./Project/Clean/indexfile.txt") as file:
text=file.read()
runs = text.split('\n\n') #split text into blocks for different runs
for i in range(num_files):
runinfotxt="There is no information to run_id "+Run_ids[i]+" in the indexfile"
for block in runs: #search block with corresponding run_id
firstline=""
for let in block:
firstline=firstline+let
if let=="\n":
break
print("FIRSTLINE: ",firstline)
if firstline ==("run_id: "+Run_ids[i]+"\n"):
runinfotxt=block
runinfo=runinfo+block
else:
pass
The problem: not the complete file is processed. 'text' includes the whole file. But when printing the length of the list 'runs' it returns 74, although my file has much more blocks.
Finding the correct block works well if it is within these first 74 blocks. Is there some kind of size limit that I am running into? I get no error messages. It just behaves as if the file was much shorter.
Edit: The number of items in 'runs' depends on the file that is read. I added some (about 15) blocks to the file and now the length of 'runs' is 94.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论