python - 每次调用函数时追加到下一行
问题描述
该函数Capture(filename)
读取输出filename.txt
并将必要的内容转储到Data.csv
文件中。但是每次我调用 Capture 函数时,被转储的数据都会被重写在同一行中。
def Capture(filename):
impedance = 0
losses = {}
frequencies = {}
Xtalk = {}
rows = []
with open(PostProcessingFolder + '\Output_' + filename +'_LOG.txt') as f:
for l in f:
if l.startswith('Impedance = '):
v = l[12:-7]
impedance = float(v)
if l.startswith('Xtalk'):
m = f.next()
n = f.next()
a = m.find('Step response Next')
b = m.find('mV', a)
frequencies[l + "Step response Next"] = str(m[a+20:b].strip())
c = n.find('Step response Fext peak')
d = n.find('@', c)
e = n.find('inches', d)
g = n.find('mV', e)
frequencies[l + "Step response Fext peak @" + str(n[d+1:e].strip()) + "inches"] = str(n[e+7:g].strip())
if l.startswith('Loss per inch'):
start = l.find('@')
stop1 = l.find('GHz', start)
stop2 = l.find('dB', start)
frequencies['filename'] = filename
frequencies['impedance (Ohms)'] = impedance
frequencies["Loss per inch @" + str(float(l[start+1:stop1].strip())) + "GHz"] = float(l[stop1+5:stop2].strip())
rows.append(frequencies)
print(rows)
df = pd.DataFrame(rows)
#df.append(df, ignore_index=True)
df.to_csv('Data.csv')
有没有办法可以在每次调用此函数时将数据添加到下一个连续行?
解决方案
您需要rows.append
在行上进行更多缩进:
虽然你有
rows = []
with open(PostProcessingFolder + '\Output_' + filename +'_LOG.txt') as f:
for l in f:
...
rows.append(frequencies)
你需要:
rows = []
with open(PostProcessingFolder + '\Output_' + filename +'_LOG.txt') as f:
for l in f:
...
rows.append(frequencies)
推荐阅读
- python - 运行 pip install firebase-admin 后出现错误
- c++ - 在 C++ 中实现二进制串行协议的最佳方法
- java - 同时,剂量 testng 将不会继续执行,直到所有已完成线程的测试?
- javascript - 如何从一周的下一天创建日期对象,hh:mm 和 utc 偏移量
- drupal - 将 rel="nofollow" 应用于 drupal 7 中的字段
- django-rest-framework - 在使用 jwt 的项目中从 django-rest-auth 迁移到 dj-rest-auth
- bootstrap-4 - 引导脚本在 Blazor 应用程序中不起作用
- elasticsearch - 搜索常用前缀关键字时快速返回
- multithreading - 查找线程启动的原因
- pandas - Pandas - 扩大平均会话时间