每次调用函数时追加到下一行

时间:2018-06-18 07:06:41

标签: python pandas

函数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')

每次调用此函数时,有没有办法可以将数据添加到下一个连续行?

3 个答案:

答案 0 :(得分:1)

您的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)

答案 1 :(得分:0)

要获得更多抓地力,请分开一些功能:

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')

答案 2 :(得分:0)

问题在于列表row的声明。每次调用方法时,它都会创建一个空列表。在方法之外声明你的列表,它应该工作正常。我建议使用基于OOPS的方法。

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.