次のコードを書いて、圧縮されたディレクトリの中のテキストファイルを読みました。私は出力をバイトで欲しくないので、出力を文字列として表示するためにTextIOWrapperを追加しました。これがzipファイルを1行ずつ読み込む正しい方法であると仮定すると(それがわからない場合)、なぜ出力は空白行を出力しますか?それを取り除く方法はありますか?TextIOWrapperを使ったpython zipfileモジュール
import zipfile
import io
def test():
zf = zipfile.ZipFile(r'C:\Users\test\Desktop\zip1.zip')
for filename in zf.namelist():
words = io.TextIOWrapper(zf.open(filename, 'r'))
for line in words:
print (line)
zf.close()
test()
>>>
This is a test line...
This is a test line...
>>>
The two lines in the file inside of the zipped folder are:
This is a test line...
This is a test line...
ありがとうございます!