[英]Read raw binary file line by line, convert it to ascii and store it in the .txt format in the same way
I am able to read raw binary data line by line but can't store it line by line into a .txt file.我能够逐行读取原始二进制数据,但无法将其逐行存储到 .txt 文件中。 Can someone please help me.
有人可以帮帮我吗。
Here is my code:这是我的代码:
with open("aa.dwr", "rb") as file:
data = file.readline()
datastring = str(data)
while data:
with open("out1.txt", "w") 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
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
Please try like this...请尝试这样...
or you can try like或者你可以试试
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
If your original data is string encoded as binary then you can read the binary and then decode it to string.如果您的原始数据是字符串编码为二进制,那么您可以读取二进制文件,然后将其解码为字符串。 Then split the string line-by-line and write it to file.
然后逐行拆分字符串并将其写入文件。
This only applies if you want to split the text as string line-by-line.这仅适用于要将文本逐行拆分为字符串的情况。 If you want to split binary line-by-line, like @Selcuk mentioned, what does that even mean?
如果您想逐行拆分二进制文件,就像@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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.