使用Python将文本的一部分数据提取到csv中

时间:2016-11-17 21:18:36

标签: python csv

我在文本文件中有一些数据,如下图所示(它只是整个数据的一部分)。筛选密钥的最佳方法是什么?

{  u'chan': 5,
   u'cls': 0,
   u'codr': u'4/5',
   u'data': u'ABfxqqqpVVVOAAA='
} 

2 个答案:

答案 0 :(得分:1)

假设样本数据是您的数据的一条记录,您可以使用pandas进行子集化并直接导出到Excel工作簿。

import pandas as pd

df = pd.DataFrame({  u'chan': 5,
   u'cls': 0,
   u'codr': u'4/5',
   u'data': u'ABfxqqqpVVVOAAA=',
   u'datr': u'SF10BW125',
   u'freq': u'912.9',
   u'lsnr': u'-8.2',
   u'mhdr': u'8007000002001900',
   u'modu': u'LORA',
   u'opts': u'',
   u'port': 5,
   u'rfch': 1,
   u'rssi': -111,
   u'seqn': 25,
   u'size': 16,
   u'timestamp': u'2016-11-17T09:51:44.406724Z',
   u'tmst': 2477375724L},index=[0])
df = df[['data','chan','timestamp','rssi']]
oName = #Path to desired excel workbook
df.to_excel(oName,'Sheet1')

答案 1 :(得分:0)

对于您发布的一系列文件,请尝试

import csv
filenames=[...]
#open your output file in append mode

with open('output.csv','a+') as out:
    outwriter = csv.writer(out,dialect='excel')

#write your headers
outwriter.writerow(['header1','header2','header3'])

#for every file
for fname in filenames:
    with open(fname) as f:

        #list that holds each row
        csvlines=[]

        #labels you wanna keep
        keep=["u'data'","u'rssi'","u'timestamp'"]
        lines=f.readlines()
        print lines

        #split at first : character
        lines = map(lambda x:x.split(':',1),lines)

        for line in lines:
            if line[0].strip() in keep:

                csvlines.append(line[1].strip())
    #clean from unnecessary characters
            csvlines=map(lambda x:x.replace("u'","").replace("'","").replace(",",""),csvlines)
    #write it to csv and then reset it for the next file.
            if(len(csvlines)==3):
               print "writing"
               print csvlines
               outwriter.writerow(csvlines)
               csvlines=[]