python split()不处理整个文本

发布于 2025-01-28 03:49:29 字数 1060 浏览 1 评论 0原文

我读了一个文本文件,上面有几个块(运行)被“ \ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文