Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个 gzip 文件,我正在尝试读取文件的行:
g = gzip.open(filename) while True: dline = g.readline() if not dline: print "<<" + dline + ">>" g.close()
上面的输出是:
<<>> <<>> <<>> <<>> ... infinitely
这里有什么问题?PS gzip 是一个 utf-8 文本文件。
您没有可能退出循环的条件。尝试:
while True: dline = g.readline() if not dline: break print "<<" + dline + ">>"
当readline()没有更多行要读取时,函数族返回一个空字符串。
readline()