[英]Adding extra column to output csv file in python
I'm having trouble with some code at the moment, I can't quite figure out how to add the year (last 4 digits of multiple filenames) as a sort of 'ID' field, using a for-loop? 我目前在某些代码上遇到麻烦,我不太清楚如何使用for循环将年份(多个文件名的后4位)添加为一种“ ID”字段? I know there are other ways of doing this but I would like to try this way as I'm learning for-loops.
我知道还有其他方法可以这样做,但是我在学习for循环时想尝试这种方法。
Code I have so far: 我到目前为止的代码:
import csv
def extract_names(filename):
inF = open(filename, 'rU')
csvF = csv.reader(inFile, delimiter=',')
# Initialization
results = []
rowNum = 0
for row in csvFile:
if rowNum != 0: #no need for first row#
#This is where the results list is appended #
records.append((row[0], row[1], "Boy"))
records.append((row[2], row[3], "Girl"))
rowNum += 1
inF.close()
return(results)
#### Start main program #####
filenames = ('file2010.csv',
'file2011.csv',
'file2012.csv',
'file2013.csv',
'file2014.csv')
outF = open('fileAll.csv','wb')
csvF_out = csv.writer(outFile, delimiter=',')
for filename in filenames:
name, ext = filename.split('.')
year = name[-4:]
results = extract_names(filename)
for line in results:
line.insert(0,year)
print("Write in csv file...")
outF.close()
Desired output: 所需的输出:
2010 | Y | X | Z
import csv
filenames = (
'file2010.csv',
'file2011.csv',
'file2012.csv',
'file2013.csv',
'file2014.csv'
)
outF = open('fileAll.csv', 'wb')
csvF_out = csv.writer(outF, delimiter=',')
def extract_content(filename):
return [("0","1","boy"),("2","3","girl")]
for filename in filenames:
name, ext = filename.split('.')
year = name[-4:]
result = extract_content(filename)
for row in result:
csvF_out.writerow((year,)+row)
outF.close()
# fileall.csv:
# 2010 | x | y | z
# 2011 | x | y | z
# 2012 | x | y | z
# 2013 | x | y | z
# 2014 | x | y | z
Where | 哪里 represents a new column.
代表一个新列。 Note that instead of using
csv.writerows
outside of the loop, the code uses csv.writerow
inside the for-loop. 请注意,代码不是在循环外部使用
csv.writerows
,而是在for循环内部使用csv.writerow
。 No additional loop is necessary. 无需其他循环。
You want to insert the year as the first cell of each line (if I understood correctly. If think that the following is self explained 您想将年份作为每行的第一个单元格插入 (如果我理解正确的话。如果认为以下内容是自我解释的话)
filenames = ('file2010.csv',
'file2011.csv',
'file2012.csv',
'file2013.csv',
'file2014.csv')
outF = open('fileAll.csv','wb')
csvF_out = csv.writer(outF, delimiter=',')
for filename in filenames:
name, ext = filename.split('.')
year = name[-4:]
result = extract_content(filename)
### Here is the loop you are looking for
for line in result: # for each line
line.insert(0, year) # insert the year as the first cell
csvF_out.writerows(reult)
outF.close()
Few other notes 其他注意事项
with open(file):
which is more secure with open(file):
看一下with open(file):
这样做更安全 2010 | Y | X | Z
2010 | Y | X | Z
2010 | Y | X | Z
2010 | Y | X | Z
while you delimiter is set to ,
in your writer. ,
当定界符设置为时, 2010 | Y | X | Z
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.