The function Capture(filename)
reads the output filename.txt
and dumps out the necessary stuff into a Data.csv
file. But each time I call the Capture function, the data that gets dumped is rewritten in the same row.
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')
Is there a way i can add the data to the next consecutive row each time this function is called?
You need more indents on your rows.append
line:
While you have
rows = []
with open(PostProcessingFolder + '\Output_' + filename +'_LOG.txt') as f:
for l in f:
...
rows.append(frequencies)
You need:
rows = []
with open(PostProcessingFolder + '\Output_' + filename +'_LOG.txt') as f:
for l in f:
...
rows.append(frequencies)
To have a bit more grip, separate some functionality:
def Capture(filename):
impedance = 0
# losses = {}
# frequencies = {}
Xtalk = {}
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())
yield frequencies
# just the rows
# print([r for r in Capture(filename)])
# build the dataframe
df = pd.DataFrame([r for r in Capture(filename)])
df.to_csv('Data.csv')
Problem is with the declaration of the list row
. Every time you are calling the method it creates an empty list. Declare your list outside the method and it should work fine. I would suggest using OOPS based approach.
class Text:
def __init__:
self.rows= list()
def capture(self):
//put your code. user rows as self.rows.append()
if __name__ = "__main__":
tob = Text()
tob.capture() // or you can call it in your own way.
//here your values of rows will persist until you rerun this program.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.