2 回答

TA貢獻1856條經驗 獲得超11個贊
with open("out1.txt", "w") as f:
while data:
f.write(' '.join(map(str,data)))
f.write("\r\n")
print("line {}: {}".format(cnt, map(str,datastring.strip())))
data = file.readline()
cnt +=1
請像這樣嘗試...
或者你可以試試
while data:
with open("out1.txt", "a") as f:
f.write(' '.join(map(str,data)))
f.write("\r\n")
print("line {}: {}".format(cnt, map(str,datastring.strip())))
data = file.readline()
cnt +=1

TA貢獻1821條經驗 獲得超6個贊
如果您的原始數據是字符串編碼為二進制文件,那么您可以讀取二進制文件,然后將其解碼為字符串。然后逐行拆分字符串并將其寫入文件。
僅當要將文本逐行拆分為字符串時,這才適用。如果你想像@Selcuk提到的那樣逐行拆分二進制,這意味著什么?
with open("file.b", "rb") as f:
data = f.read()
data = data.decode()
lines = data.splitlines()
with open("out.txt", "w") as f2:
for line in lines:
f2.write(line + "\n")
添加回答
舉報