CSV文件和字典

时间:2015-10-05 14:25:35

标签: python python-2.7

我正在尝试创建CSV文件,基于迭代其他CSV文件。输出CSV文件的格式与输入文件略有不同。

我的问题是,对Python来说还是新手,我该怎么做呢?

我的输入是这样的:

1.csv
"Street", "Number", "Occupants"
"Test Road", "7", "4"
"Test Street", "5", "1"
"Test Avenue, "2", "6"

2.csv
"Street", "Number", "Occupants"
"Test Road", "12", "2"
"Test Street", "11", "3"
"Test Avenue, "9", "2"

3.csv
"Street", "Number", "Occupants"
"Test Road", "34", "2"
"Test Street", "22", "3"
"Test Lane", "19", "2"

expected_output.csv
"", "Street", "1", "2", "3"
"Number", "Test Road", "7", "12", "34"
"", "Test Street", "5", "11", "22"
"", "Test Avenue", "2", "9", "N/A"
"", "Test Lane", "N/A", "N/A", "19"
"Occupants", "Test Road", "4", "2", "2"
"", "Test Street", "1", "3", "3"
"", "Test Avenue", "6", "2", "N/A"
"", "Test Lane", "N/A", "N/A", "2"

所以你可以看到输出的顶部我有Number/OccupantsStreet然后输入cvs文件的编号(1.csv,2.csv,3.csv等)< / p>

如果特定街道不在输入文件中,则输出应显示N/ANumber

Occupant

我不确定最好/最快的方法是什么。首先将所有CSV文件连接在一起,也可以在字典中连接,然后执行一些复杂的循环或者使用多个循环来创建输出。

1 个答案:

答案 0 :(得分:1)

根据我们在聊天中的讨论,这是一个完整的程序,包括一些测试文件。您只需要删除标记的行以使用您自己的文件。

#user configurable variables:
number_of_files = 4

#delete the following lines to use your own files.
f1 = open('1.csv','w')
f1.write('''"Street", "Number", "Occupants"
"Test Road", "7", "4"
"Test Street", "5", "1"
"Test Avenue", "2", "6"''')
f1.close()

f2 = open('2.csv','w')
f2.write('''"Street", "Number", "Occupants"
"Test Road", "12", "2"
"Test Street", "11", "3"
"Test Avenue", "9", "2"''')
f2.close()

f3 = open('3.csv','w')
f3.write('''"Street", "Number", "Occupants"
"Test Road", "34", "2"
"Test Street", "22", "3"
"Test Lane", "19", "2"''')
f3.close()

f4 = open('4.csv','w')
f4.write('''"Street", "Number", "Occupants"
"Test Road", "4", "7"
"Test Street", "1243", "6"
"Test Lane", "17", "1"''')
f4.close()
#stop deleteing

#the rest 'should' take care of itself.
file_set = []
for y in range(number_of_files):
    exec("f"+str(y+1)+" = [x.split(',') for x in open('"+str(y+1)+".csv','r').read().replace('\"','').replace(', ',',').split('\\n')]")
    exec("file_set.append(f"+str(y+1)+")")

num_files = len(file_set)

data_dict = {}

for file in file_set:
    block = file_set.index(file)

    for line in file:
        row = file.index(line)

        if line[0] not in data_dict:
            data_dict[line[0]] = num_files*[['N/A','N/A']]

        data_dict[line[0]][block] = line[1:]

print (data_dict)


f0 = open('output.csv','w')
f0.write(',Street')
for x in range(num_files):
    f0.write(','+str(x+1))
f0.write('\n')

temp = 0
for key in data_dict:
    if key != 'Street':
        if temp == 0:
            f0.write('Number,'+key)
            temp = 1
        else:
            f0.write(','+key)
        for x in range(num_files):
            f0.write(','+data_dict[key][x][0])
        f0.write('\n')

temp = 0
for key in data_dict:
    if key != 'Street':
        if temp == 0:
            f0.write('Occupants,'+key)
            temp = 1
        else:
            f0.write(','+key)
        for x in range(num_files):
            f0.write(','+data_dict[key][x][1])
        f0.write('\n')

f0.close()

享受,度过美好的一天。