python csv复制列
Posted
技术标签:
【中文标题】python csv复制列【英文标题】:python csv copy column 【发布时间】:2013-10-19 22:29:05 【问题描述】:我有一个包含以下内容的文件
first_name,last_name,uid,email,dep_code,dep_name
john,smith,jsmith,jsmith@gmail.com,finance,21230
john,king,jking,jjing@gmail.com,human resource,31230
我想复制列“email”并创建一个新列“email2”,然后将 gmail.com 从列 email2 替换为 hotmail.com
我是 python 新手,所以需要专家的帮助,我尝试了几个脚本,但如果有更好的方法,请告诉我。原始文件包含 60000 行。
with open('c:\\Python27\\scripts\\colnewfile.csv', 'rb') as fp_in1, open('c:\\Python27\\scripts\\final.csv', 'wb') as fp_out1:
writer1 = csv.writer(fp_out1, delimiter=",")
reader1 = csv.reader(fp_in1, delimiter=",")
domain = "@hotmail.com"
for row in reader1:
if row[2:3] == "uid":
writer1.append("Email2")
else:
writer1.writerow(row+[row[2:3]])
这是最终的脚本,唯一的问题是它没有完成整个输出文件,它只显示 61409 行,而在输入文件中有 61438 行。
inFile = 'c:\Python27\scripts\in-093013.csv' outFile = 'c:\Python27\scripts\final.csv'
open(inFile, 'rb') 作为 fp_in1,open(outFile, 'wb') 作为 fp_out1: writer = csv.writer(fp_out1, delimiter=",") reader = csv.reader(fp_in1, delimiter=",") 对于阅读器中的 col: 德尔科尔[6:] writer.writerow(col) 标题 = 下一个(读者) writer.writerow(标题 + ['email2']) 对于阅读器中的行: 如果长度(行)> 3: 电子邮件 = email.split('@', 1)[0] + '@hotmail.com' writer.writerow(row + [email])
【问题讨论】:
【参考方案1】:如果您在阅读器上调用next()
,您一次只能获得一行;用它来复制标题。复制电子邮件栏很容易:
import csv
infilename = r'c:\Python27\scripts\colnewfile.csv'
outfilename = r'c:\Python27\scripts\final.csv'
with open(infilename, 'rb') as fp_in, open(outfilename, 'wb') as fp_out:
reader = csv.reader(fp_in, delimiter=",")
headers = next(reader) # read first row
writer = csv.writer(fp_out, delimiter=",")
writer.writerow(headers + ['email2'])
for row in reader:
if len(row) > 3:
# make sure there are at least 4 columns
email = row[3].split('@', 1)[0] + '@hotmail.com'
writer.writerow(row + [email])
此代码在第一个@
符号上拆分电子邮件地址,获取拆分的第一部分并在其后添加@hotmail.com
:
>>> 'example@gmail.com'.split('@', 1)[0]
'example'
>>> 'example@gmail.com'.split('@', 1)[0] + '@hotmail.com'
'example@hotmail.com'
以上产生:
first_name,last_name,uid,email,dep_code,dep_name,email2
john,smith,jsmith,jsmith@gmail.com,finance,21230,jsmith@hotmail.com
john,king,jking,jjing@gmail.com,human resource,31230,jjing@hotmail.com
用于您的示例输入。
【讨论】:
这是我收到的错误 email = row[3] IndexError: list index out of range @user2820987:那么您的输入文件中有空行,或者至少行太短。我会调整的。 很可能 last 行是空的;其余数据写入成功。 inFile = 'c:\\Python27\\scripts\\in-093013.csv' outFile = 'c:\\Python27\\scripts\\final.csv' with open(inFile, ' rb') as fp_in1, open(outFile, 'wb') as fp_out1: writer = csv.writer(fp_out1, delimiter=",") reader = csv.reader(fp_in1, delimiter=",") for col in reader: del col[6:] writer.writerow(col) headers = next(reader) writer.writerow(headers + ['email2']) for row in reader: if len(row) > 3: email = email.split(' @', 1)[0] + '@hotmail.com' writer.writerow(row + [email]) 你不能循环一个打开的reader
对象两次;如果您想从输出中删除一列,请在一个循环中执行此操作,如果您也添加一列。【参考方案2】:
这可以使用 pandas 非常干净地完成。就是这样:
In [1]: import pandas as pd
In [3]: df = pd.read_csv('your_csv_file.csv')
In [4]: def rename_email(row):
...: return row.email.replace('gmail.com', 'hotmail.com')
...:
In [5]: df['email2'] = df.apply(rename_email, axis=1)
In [6]: """axis = 1 or ‘columns’: apply function to each row"""
In [7]: df
Out[7]:
first_name last_name uid email dep_code dep_name email2
0 john smith jsmith jsmith@gmail.com finance 21230 jsmith@hotmail.com
1 john king jking jjing@gmail.com human resource 31230 jjing@hotmail.com
In [8]: df.to_csv('new_update_email_file.csv')
【讨论】:
以上是关于python csv复制列的主要内容,如果未能解决你的问题,请参考以下文章
我有一个大型 CSV 文件,其中包含单个列中的信息。如何使用 python 在 excel 中复制“文本到列”任务? [复制]
如何在python中将csv文件导入为numpy.array? [复制]