我有一个从API返回的字节字符串,并存储在response.content中。
使用小的内容,我可以使用下面的代码将它保存到一个没有问题的文件中
with open(save_path, 'wb') as save_file:
save_file.write(response.content)但是对于较大的文件,它将导致内存错误,因此我尝试不使用以下代码一次性读取内容。
with open(save_path, 'wb') as save_file:
for x in response.content:
save_file.write(bytes(x)) #the x from iteration seem to be converted to int so I convert it back但是上面的方法似乎替代了内容,因为它不再与另一个库兼容(在我的Laspy尝试读取保存的文件时,laspy.util.LaspyException: Invalid format: h0.0错误出现了)
我该怎么做呢?
发布于 2021-02-19 03:51:05
我看到你在使用bytes(x)时遇到的问题。将其改为x.to_bytes(1, 'big')解决您的问题
使用下面的代码显示有什么不同
a = b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'
a.decode('utf-8') # τoρνoς
with open('./save.txt', 'wb') as save_file:
for i in a:
print(i.to_bytes(1, 'big')) # write it to file, not the others
print(i)
print(bytes(i))
print('----')

https://stackoverflow.com/questions/66271219
复制相似问题