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")
添加回答
举报