我正在尝试使用python脚本从示波器捕获数据。该脚本将其另存为csv格式。我需要在开头添加几行文字来描述数据。
我查看了现有线程,看是否有可能的解决方案。我刚刚开始学习Python。我正在使用仪器随附的代码。
这是脚本的一部分,该脚本将数据另存为csv。
NewD = (np.insert(Wav_Data, 0, DataTime, axis = 0)).T
filename = BASE_DIRECTORY + BASE_FILE_NAME + ".csv"
now = time.time() # Only to show how long it takes to save
with open(filename, 'w') as filehandle:
np.savetxt(filename, NewD, delimiter = ',', header = column_titles)
我尝试从其他代码中使用以下部分,但不确定如何将其附加到csv文件中。
with open("notes.txt") as f:
NOTES = f.readlines()
NOTES = "".join(NOTES)
找不到与脚本位于同一目录中的notes.txt。 渴望听到您的反馈。预先感谢。
已更新至:
# Save data
NewD = (np.insert(Wav_Data, 0, DataTime, axis = 0)).T
filename = BASE_DIRECTORY + BASE_FILE_NAME + ".csv"
with open("notes.txt") as f:
NOTES = f.readlines()
NOTES = "".join(NOTES)
with open(filename, "a") as fh:
fh.write(NOTES)
now = time.time() # Only to show how long it takes to save
with open(filename, 'w') as filehandle:
np.savetxt(filename, NewD, delimiter = ',', header = column_titles)
答案 0 :(得分:0)
只需打开文件进行追加或写入
如果您想先编写CSV,请注意:
with open("notes.txt") as f:
NOTES = f.readlines()
NOTES = "".join(NOTES)
with open(filename, "w") as fh:
fh.write(NOTES)
# this time we give np the opened filehandle, not the filename
np.savetxt(fh, NewD, delimiter = ',', header = column_titles)