python csv copy列 [英] python csv copy column

查看:176
本文介绍了python csv copy列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  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
对于reader1中的行:
[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'



打开(inFile,'rb')为fp_in1,open(outFile,'wb')为fp_out1:
writer = csv .writer(fp_out1,delimiter =,)
reader = csv.reader(fp_in1,delimiter =,)
读取器中的col:
del col [6:]
writer.writerow(col)
header = next(阅读器)
writer.writerow(headers + ['email2'])
读取器中的行:
如果len row)> 3:
email = email.split('@',1)[0] +'@ hotmail.com'
writer.writerow(row + [email])

解决方案

如果在阅读器上调用 next() ;使用它来复制头。复制电子邮件栏很简单:

  import csv 

infilename = r'c:\ Python27 \scripts\colnewfile.csv'
outfilename = r'c:\Python27\scripts\final.csv'

打开(infilename,'rb')as fp_in,open(outfilename,'wb')as fp_out:
reader = csv.reader(fp_in,delimiter =,)
headers = next(reader)#读取第一行

writer = csv.writer(fp_out,delimiter =,)
writer.writerow(headers + ['email2'])

读取行:
如果len(row)> 3:
#确保有至少4列
email = row [3] .split('@',1)[0] +'@ hotmail.com'
writer。这个代码将电子邮件地址分成第一个<$ c $($)

$ p $ c> @
sign,接受拆分的第一部分,并在其后添加 @ hotmail.com

 >>> 'example@gmail.com'.split('@',1)[0] 
'example'
>>>> 'example@gmail.com'.split('@',1)[0] +'@ hotmail.com'
'example@hotmail.com'
pre>

以上产生:

  first_name,last_name,uid,电子邮件,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


I have a file containing following

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

I want to copy column "email" and create a new column "email2" and then replace gmail.com from column email2 to hotmail.com

I'm new to python so need help from experts, I tried few script, but if there is a better way to do it then please let me know. The original file contains 60000 rows.

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]])

Here is the final script, only problem is that it does not complete the entire outfile, it only show 61409 rows, whereas in the input file there are 61438 rows.

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])

解决方案

If you call next() on the reader you get one row at at a time; use that to copy over the headers. Copying the email column is easy enough:

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])

This code splits the email address on the first @ sign, takes the first part of the split and adds @hotmail.com after it:

>>> 'example@gmail.com'.split('@', 1)[0]
'example'
>>> 'example@gmail.com'.split('@', 1)[0] + '@hotmail.com'
'example@hotmail.com'

The above produces:

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

for your sample input.

这篇关于python csv copy列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆