manupulating csv文件并将其输出写入python

时间:2016-07-11 17:58:38

标签: python csv

我有一个名为saleem.csv的简单文件,其中包含以下csv信息行:

File,Run,Module,Name,,,,,  
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].nic.phy,nbFramesWithInterference,0,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].nic.phy,nbFramesWithoutInterference,0,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].nic.phy,nbFramesWithInterferencePartial,0,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].nic.phy,nbFramesWithoutInterferencePartial,0,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].nic.phy,nbFramesWithInterferenceDropped,0,NaN,NaN,NaN,NaN  
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].nic.phy,nbFramesWithoutInterferenceDropped,0,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].appl,broadcast queued,3,NaN,NaN,NaN,NaN  
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].appl,replies sent,0,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].appl,replies received,0,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].batteryStats,nominal,1.188e+07,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].batteryStats,total,1232.22,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].batteryStats,lifetime,-1,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].batteryStats,Mean power consumption,55.7565,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].batteryStats,num devices,1,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].batteryStats,physical layer,0,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].batteryStats,device total (mWs),1232.22,NaN,NaN,NaN,NaN
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].batteryStats,account,0,1,2,3,4
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].batteryStats,energy (mWs),0,207.519,1024.7,0,0
General-0.sca,General-0-20160706-14:58:51-10463,MyNetwork.node[0].batteryStats,time (s),0,3.83442,18.2656,0,

我想跳过第一行,读取此文件,只将列[2]和列[4]写入名为out.csv的新csv文件。我已经写了以下脚本来完成这项工作。

import csv

with open('saleem.csv') as f:
    readcsv = csv.reader(f)

    for row in readcsv:
            dele = (row[2], row[4])
            print dele
with open('out.csv', 'w+') as j:
    writecsv = csv.writer(j)
    #for row in dele:
    for row in dele:
            writecsv.writerows(dele)

f.close()
j.close()

这会产生以下输出:

M,y,N,e,t,w,o,r,k,.,n,o,d,e,[,4,],.,b,a,t,t,e,r,y,S,t,a,t,s
0
M,y,N,e,t,w,o,r,k,.,n,o,d,e,[,4,],.,b,a,t,t,e,r,y,S,t,a,t,s
0

请帮助我,对不起以前的错误,因为我错误地写了一行。 谢谢

2 个答案:

答案 0 :(得分:2)

已编辑以反映经修订的问题

我可以看到一些问题:

P1:writerows(...)

for row in dele:
            writecsv.writerows(dele)

writerows获取要写入csv文件的行列表。所以它不应该在循环中,你遍历所有行并尝试单独写它们。


P2:覆盖

for row in readcsv:
            dele = (row[2], row[4])

您不断覆盖dele,因此您无法从每一行跟踪row[2]row[4]


你能做什么呢?

dele = []
with open('saleem.csv') as f:
    readcsv = csv.reader(f)
    for row in readcsv:
        dele.append([row[2], row[4])
        print([row[2], row[4]])

with open('out.csv', 'w+') as j:
    writecsv.csvwriter(j)
    writecsv.writerows(dele)

这产生了输出:

MyNetwork.node[0].nic.phy,0
MyNetwork.node[0].nic.phy,0
MyNetwork.node[0].nic.phy,0
MyNetwork.node[0].nic.phy,0
MyNetwork.node[0].nic.phy,0
MyNetwork.node[0].nic.phy,0
MyNetwork.node[0].appl,3
MyNetwork.node[0].appl,0
MyNetwork.node[0].appl,0
MyNetwork.node[0].batteryStats,1.188e+07
MyNetwork.node[0].batteryStats,1232.22
MyNetwork.node[0].batteryStats,-1
MyNetwork.node[0].batteryStats,55.7565
MyNetwork.node[0].batteryStats,1
MyNetwork.node[0].batteryStats,0
MyNetwork.node[0].batteryStats,1232.22
MyNetwork.node[0].batteryStats,0
MyNetwork.node[0].batteryStats,0
MyNetwork.node[0].batteryStats,0


此外,与您手头的问题无关,以下代码是不必要的:

f.close()
j.close()

with open(...):语法如此广泛使用的原因是因为它可以正常地为您关闭文件。您不需要自己单独关闭它。只要with块结束,文件就会关闭。

答案 1 :(得分:0)

我建议使用pandas库。

这使得使用csv文件非常容易。

import pandas as pd #standard convention for importing pandas

# reads the csv file into a pandas dataframe
dataframe = pd.read_csv('saleem.csv') 

# make a new dataframe with just columns 2 and 4 
print_dataframe = dataframe.iloc[:,[2,4]] 

# output the csv file, but don't include the index numbers or header, just the data
print_dataframe.to_csv('out.csv', index=False, header=False) 

如果您使用Ipython或Jupyter Notebook,则可以输入

dataframe.head()

查看数据帧的前几个值。您可以使用可能值得学习的库来做更多事情,但一般来说,它是读取,过滤和处理csv数据的好方法。