Python 向现有 csv 文件添加新列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23682236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add a new column to an existing csv file
提问by user3641397
I have a csv file with 5 columns and I want to add data in a 6th column. The data I have is in an array.
我有一个包含 5 列的 csv 文件,我想在第 6 列中添加数据。我拥有的数据在一个数组中。
Right now, the code that I have will insert the data I would want in the 6th column only AFTER all the data that already exists in the csv file.
现在,只有在 csv 文件中已经存在的所有数据之后,我拥有的代码才会在第 6 列中插入我想要的数据。
For instance I have:
例如我有:
wind, site, date, time, value
10, 01, 01-01-2013, 00:00, 5.1
89.6 ---> this is the value I want to add in a 6th column but it puts it after all the data from the csv file
Here is the code I am using:
这是我正在使用的代码:
csvfile = 'filename'
with open(csvfile, 'a') as output:
writer = csv.writer(output, lineterminator='\n')
for val in data:
writer.writerow([val])
I thought using 'a'
would append the data in a new column, but instead it just puts it after ('under') all the other data... I don't know what to do!
我认为 using'a'
会将数据附加到一个新列中,但它只是将它放在('under')所有其他数据之后......我不知道该怎么做!
回答by Jonathan Eskritt
The append mode of opening files is meant to add data to the end of a file. what you need to do is provide random access to your file writing. you need to use the seek() method
打开文件的附加模式旨在将数据添加到文件的末尾。您需要做的是提供对文件写入的随机访问。你需要使用 seek() 方法
you can see and example here: http://www.tutorialspoint.com/python/file_seek.htm
你可以在这里看到和例子:http: //www.tutorialspoint.com/python/file_seek.htm
or read the python docs on it here: https://docs.python.org/2.4/lib/bltin-file-objects.htmlwhich isn't terribly useful
或者在这里阅读 python 文档:https: //docs.python.org/2.4/lib/bltin-file-objects.html这不是非常有用
if you want to add to the end of a column you may want to open the file read a line to figure out it's length then seek to the end.
如果您想添加到列的末尾,您可能需要打开文件读取一行以确定它的长度,然后寻找到末尾。
回答by Steven Rumbalski
Appending writes data to the end of a file, not to the end of each row.
追加将数据写入文件的末尾,而不是每行的末尾。
Instead, create a new file and append the new value to each row.
相反,创建一个新文件并将新值附加到每一行。
csvfile = 'filename'
with open(csvfile, 'r') as fin, open('new_'+csvfile, 'w') as fout:
reader = csv.reader(fin, newline='', lineterminator='\n')
writer = csv.writer(fout, newline='', lineterminator='\n')
if you_have_headers:
writer.writerow(next(reader) + [new_heading])
for row, val in zip(reader, data)
writer.writerow(row + [data])
On Python 2.x, remove the newline=''
arguments and change the filemodes from 'r'
and 'w'
to 'rb'
and 'wb'
, respectively.
在 Python 2.x 上,删除newline=''
参数并将文件模式分别从'r'
和更改'w'
为'rb'
和'wb'
。
Once you are sure this is working correctly, you can replace the original file with the new one:
一旦您确定这正常工作,您可以用新文件替换原始文件:
import os
os.remove(csvfile) # not needed on unix
os.rename('new_'+csvfile, csvfile)
回答by fred.yu
csv
module does not support writing or appending column. So the only thing you can do is: read from one file, append 6th column data, and write to another file. This shows as below:
csv
模块不支持写入或附加列。所以你唯一能做的就是:从一个文件读取,附加第 6 列数据,然后写入另一个文件。这显示如下:
with open('in.txt') as fin, open('out.txt', 'w') as fout:
index = 0
for line in iter(fin.readline, ''):
fout.write(line.replace('\n', ', ' + str(data[index]) + '\n'))
index += 1
data
is a int
list.
data
是一个int
列表。
I test these codes in python, it runs fine.
我在 python 中测试了这些代码,它运行良好。