Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用str.read()时卡住_Python_Python 3.x_File - Fatal编程技术网

Python 使用str.read()时卡住

Python 使用str.read()时卡住,python,python-3.x,file,Python,Python 3.x,File,我是python新手。我不知道为什么当我使用str.read()时,所有字符都像这样存储在每一行中 使用fname.readlines()代替fname.read() 将fr=fname.read()更改为fr=fname.read().splitlines()将代码作为文本放在答案中,而不是图像您应该直接迭代文件对象以迭代行。在任何情况下,请阅读以下内容。寻求调试帮助的问题必须提供答案,并且答案必须包含在问题中。将所有代码发布为@user10987432,这是一种反模式。如果您真的想这样做,您

我是python新手。我不知道为什么当我使用str.read()时,所有字符都像这样存储在每一行中

使用fname.readlines()代替fname.read()


fr=fname.read()
更改为
fr=fname.read().splitlines()
将代码作为文本放在答案中,而不是图像您应该直接迭代文件对象以迭代行。在任何情况下,请阅读以下内容。寻求调试帮助的问题必须提供答案,并且答案必须包含在问题中。将所有代码发布为@user10987432,这是一种反模式。如果您真的想这样做,您应该直接使用
fr=list(fname)
,但几乎可以肯定的是,您可以直接使用file对象,不将整个内容读取到内存中。请小心使用裸露的
,除了这样的
,请参阅。另外,我建议使用上下文管理器来处理文件对象。
while True:
    fhand = input('Enter a file name:\n')
    try:
        fname = open(fhand)
        break
    except:
        print('Please enter file name again in doc C')
    continue
fr = fname.read()
for line in fr:
    line = line.rstrip()
    if not line.startwith('From')
        print(line)