我在文本文件中设置了这些字典: dataset 1
我在另一个文本文件中也有这些数据集: dataset 2
我的代码:
import csv
#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(['data','rssi','time'])
with open("data2.txt") as f:
#list that holds each row
csvlines=[]
#labels you wanna keep
keep=["u'dataFrame'","u'rssi'","u'timestamp'"]
lines=f.readlines()
#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=[]
我的目的是将特定数据过滤到csv文件中,但它适用于我上面显示的第二个样本数据,但不适用于第一个样本数据..看起来它们与我完全相同。为什么第一个不使用此代码?