读取文件直到python中的特定字符

Tar*_*run 3 python python-3.x

我当前正在开发一个应用程序,该应用程序需要从文件中读取所有输入,直到遇到某个字符为止。

通过使用代码:

file=open("Questions.txt",'r')
c=file.readlines()
c=[x.strip() for x in c]
Run Code Online (Sandbox Code Playgroud)

每次遇到条带时\n,都会将其从输入中删除并视为list中的字符串c

这意味着每一行都被分成列表的一部分c。但是我想列出一个遇到任何特殊字符的列表

如果输入文件包含以下内容:

1.Hai
2.Bye\-1
3.Hello
4.OAPd\-1
Run Code Online (Sandbox Code Playgroud)

然后我想得到一个清单 c=['1.Hai\n2.Bye','3.Hello\n4.OApd']

请帮助我做到这一点。

Alf*_*lfe 7

最简单的方法是将文件作为单个字符串读取,然后将其拆分为分隔符:

with open('myFileName') as myFile:
  text = myFile.read()
result = text.split(separator)  # use your \-1 (whatever that means) here
Run Code Online (Sandbox Code Playgroud)

如果您的文件很大,.split()则不希望将完整的内容作为单个字符串保存在内存中(然后也不希望在拆分后将完整的内容保存在列表中)。然后,您可以分块阅读它:

def each_chunk(stream, separator):
  buffer = ''
  while True:  # until EOF
    chunk = stream.read(CHUNK_SIZE)  # I propose 4096 or so
    if not chunk:  # EOF?
      yield buffer
      break
    buffer += chunk
    while True:  # until no separator is found
      try:
        part, buffer = buffer.split(separator, 1)
      except ValueError:
        break
      else:
        yield part

with open('myFileName') as myFile:
  for chunk in each_chunk(myFile, separator='\\-1\n'):
    print(chunk)  # not holding in memory, but printing chunk by chunk
Run Code Online (Sandbox Code Playgroud)

  • 使用 [`partition`](https://docs.python.org/3/library/stdtypes.html#str.partition) 而不是 [`split`](https://docs.python.org/3/library /stdtypes.html#str.split)可能会更快。 (2认同)