简体   繁体   English

在python中添加带有新列的CSV文件

[英]Appending a CSV file with a new column in python

I am trying to create a clean csv file by merging some of variables together from an old file and appending them to a new csv file. 我正在尝试通过合并一些旧文件中的变量并将它们附加到新的csv文件中来创建干净的csv文件。

I have no problem running the data the first time. 我第一次运行数据没有问题。 I get the output I want but whenever I try to append the data with a new variable (ie new column) it appends the variable to the bottom and the output is wonky. 我得到了想要的输出,但是每当我尝试向数据附加新变量(即新列)时,它将变量附加到底部,输出就很奇怪。

I have basically been running the same code for each variable, except changing the groupvariables variable to my desired variables and then using the f2= open('outputfile.csv', "ab") <--- but with an ab for amend. 除了将groupvariables变量更改为所需的变量,然后使用f2 = open('outputfile.csv',“ ab”)<---,但使用ab进行修改外,我基本上为每个变量运行了相同的代码。 Any help would be appreciated 任何帮助,将不胜感激

groupvariables=['x','y']

f2  = open('outputfile.csv', "wb")
writer = csv.writer(f2, delimiter=",")
writer.writerow(("ID","Diagnosis"))

for line in csv_f:
    line = line.rstrip('\n')
    columns  = line.split(",")
    tempname = columns[0]
    tempindvar = columns[1:]

templist = []

for j in groupvariables:
    tempvar=tempindvar[headers.index(j)]
    if tempvar != ".":
        templist.append(tempvar)

newList = list(set(templist))

if len(newList) > 1:
    output = 'nomatch'
elif len(newList) == 0:
    output = "."
else:
    output = newList[0]

tempoutrow = (tempname,output)
writer.writerow(tempoutrow)

f2.close() f2.close()

CSV is a line-based file format, so the only way to add a column to an existing CSV file is to read it into memory and overwrite it entirely, adding the new column to each line. CSV是基于行的文件格式,因此,将列添加到现有CSV文件的唯一方法是将其读入内存并完全覆盖,然后将新列添加到每一行。

If all you want to do is add lines , though, appending will work fine. 但是,如果您只想添加 ,则追加会很好。

Here is something that might help. 这可能会有所帮助。 I assumed the first field on each row in each csv file is a primary key for the record and can be used to match rows between the two files. 我假设每个csv文件中每一行的第一个字段都是记录的主键,可用于匹配两个文件之间的行。 The code below reads the records in from one file, stored them in a dictionary, then reads in the records from another file, appended the values to the dictionary, and writes out a new file. 下面的代码从一个文件中读取记录,将它们存储在字典中,然后从另一个文件中读取记录,将值附加到字典中,并写出一个新文件。 You can adapt this example to better fit your actual problem. 您可以调整此示例以更好地解决您的实际问题。

import csv
# using python3

db = {}
reader = csv.reader(open('t1.csv', 'r'))
for row in reader:
    key, *values = row
    db[key] = ','.join(values)

reader = csv.reader(open('t2.csv', 'r'))
for row in reader:
    key, *values = row
    if key in db:
        db[key] = db[key] + ',' + ','.join(values)
    else:
        db[key] = ','.join(values)

writer = open('combo.csv', 'w')
for key in sorted(db.keys()):
    writer.write(key + ',' + db[key] + '\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM