python csv复制列

我有一个包含以下内容的文件

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

我想复制列“电子邮件”,并创建一个新的列“email2”,然后从列email2替换gmail.com到hotmail.com

我是python的新手,所以需要专家的帮助,我尝试了几个脚本,但是如果有更好的方法去做,那么请让我知道。 原始文件包含60000行。

with open('c:Python27scriptscolnewfile.csv', 'rb') as fp_in1, open('c:Python27scriptsfinal.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]])

这里是最后一个脚本,唯一的问题是它没有完成整个outfile,它只显示61409行,而在输入文件中有61438行。

inFile ='c: Python27 scripts in-093013.csv'outFile ='c: Python27 scripts final.csv'

打开(outFile,'wb')作为fp_out1:writer = csv.writer(fp_out1,delimiter =“,”)阅读器= csv.reader(fp_in1,delimiter =“,”)打开(inFile,'rb')作为fp_in1, )for col in reader:del col [6:] writer.writerow(col)headers = next(reader)writer.writerow(headers + ['email2'])用于阅读器中的行:if len(row)> 3:email = email.split('@',1)[0] +'@ hotmail.com'writer.writerow(row + [email])


如果您在阅读器上打电话给next() ,您一次只能看到一行; 用它来复制标题。 复制电子邮件栏非常简单:

import csv

infilename = r'c:Python27scriptscolnewfile.csv'
outfilename = r'c:Python27scriptsfinal.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

为您的示例输入。


这可以使用熊猫非常干净地完成。 这里是:

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')
链接地址: http://www.djcxy.com/p/55091.html

上一篇: python csv copy column

下一篇: Avoiding quotes around row of string data with csv.writer