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

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

最佳答案

对于您发布内容形式的一系列文件,请尝试

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=[]

09-06 23:43