在python中处理多个CSV文件

时间:2015-05-20 06:21:10

标签: python csv

我尝试从几个不同的csv文件中提取数据并将其插入到1个CSV文件中,但我无法创建一个具有每个字段的特定名称的列表(记录)。 / p>

原始文件格式: [女孩名字,女孩名字数,男孩名字,女孩名数]

输出: [姓名,统计,性别]

我到目前为止的代码是:

import csv

def extract_names(filename):

    inFile = open(filename, 'rU')
    csvFile = csv.reader(inFile, delimiter=',')

    # Initialization
    records = []
    rowNum = 0

    for row in  csvFile:

        if rowNum != 0:

            # Read each row of csv file and save information  in list 'records'
            # as (name, count, gender) 
            records.append(name, count, gender)


            print('Process each row...')      

        rowNum += 1

    inFile.close()
    return(records)
启动主程序
filenames = ('babyQld2010.csv', 'babyQld2011.csv', 'babyQld2012.csv', 'babyQld2012.csv', 'babyQld2014.csv')

outFile = open('babyQldAll.csv','w') 
csvFile_out = csv.writer(outFile, delimiter=',')

for filename in filenames:
    name, ext = filename.split('.')
    year = name[-4:]     #extract year from  file names
    records = extract_names(filename)


    # Get (name, count, gender)  from  list "records", 
    # and add value of "year" and write into output file (using "for" loop )

    print("Write in csv file...")     

outFile.close()

3 个答案:

答案 0 :(得分:0)

假设你的第一个csv在第一行中有列名:

import csv

res = []
infile = open(filename, "r")
oufile = open(oufilename, "w")
inf = csv.reader(infile)
inf.next()
ouf = csv.writer(oufile)
ouf.writerow(["name", "count", "gender"])
for row in inf:
    res.append([row[0], row[1], "F"])
    res.append([row[2], row[3], "M"])
ouf.writerows(res)
infile.close()
oufile.close()

答案 1 :(得分:0)

使用DictReader:

[burhan@dev t]$ cat sample.csv
girl name, count of girl names, boy name, count of boynames
Laura,1,John,2
Sarah,3,Frank,5
[burhan@dev t]$ python
>>> import csv
>>> with open('sample.csv') as f:
...   reader = csv.DictReader(f)
...   rows = list(reader)
...
>>> rows[0]['girl name']
'Laura'
>>> rows
[{' count of girl names': '1', ' count of boynames': '2', ' boy name': 'John', 'girl name': 'Laura'}, {' count of girl names': '3', ' count of boynames': '5', ' boy name': 'Frank', 'girl name': 'Sarah'}]

如你所说,你不能使用DictReader,试试这个版本:

import csv

results = list()

with open('file.csv') as f:
   reader = csv.reader(f, delimiter=',')
   header = next(reader) # Saves the first line and skips it
   for row in reader:
      results.append(dict(zip(header,row)))

print(results)

答案 2 :(得分:0)

这样可行。您仍然应该在所有csv文件上构建一个循环,构建一个安全机制,以防您有一个csv文件,其中包含不等数量的女孩和男孩名称,并且可能修剪一些空格。

import csv
def read_gender_counts():
    sample_csv = 'GirlName, GirlCount, BoyName, BoyCount\nAnna, 4, John, 6\nMary, 5, Fred, 10'
    csvFile = csv.reader(sample_csv.split('\n'), delimiter=',')

    records = list()
    for i, row in enumerate(csvFile):
        if i > 0:
            records.append((row[0], row[1], 'F'))
            records.append((row[2], row[3], 'M'))

    print (records)

输出:

[('Anna', ' 4', 'F'), 
 (' John', ' 6', 'M'), 
 ('Mary', ' 5', 'F'), 
 (' Fred', ' 10', 'M')]