【发布时间】:2017-06-12 18:10:14
【问题描述】:
我正在尝试将多个二进制文件转换为一个 CSV 文件。如果我对文件使用'w'#write,我的代码就可以工作,但是每次新的迭代都会覆盖最后一个。但是,当我使用 'a' #add 到文件时,我的结果值与使用 'w' 时不同(并且不正确)。有没有办法在不使用追加的情况下将我的结果放入一个文件而不覆盖以前的结果?
这是我的代码:
import os
import numpy as np
fileLib1 = ('/path1/')
ref = ('/path2/ref.csv')
for file in os.scandir(fileLib1):
with open(file,'rb') as f:
text = list(np.fromfile(f,dtype=np.float32))
with open(ref,'a') as conv: #problem, 'a' vs 'w'
for n in text:
conv.write('%s,\n' %n)
【问题讨论】:
-
使用
open(file, 'ab')代替编写二进制文件。 -
'ab' 给了我问题,'内核死了,重新启动'。到目前为止,“rb”似乎对我有用。
标签: python python-3.x io