2 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
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
請(qǐng)像這樣嘗試...
或者你可以試試
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貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果您的原始數(shù)據(jù)是字符串編碼為二進(jìn)制文件,那么您可以讀取二進(jìn)制文件,然后將其解碼為字符串。然后逐行拆分字符串并將其寫入文件。
僅當(dāng)要將文本逐行拆分為字符串時(shí),這才適用。如果你想像@Selcuk提到的那樣逐行拆分二進(jìn)制,這意味著什么?
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")
添加回答
舉報(bào)