我有两个列,我想分开,首先我摆脱任何有Jr和II的东西,哪个有效,然后我想把名字分成一个单独的标签
我有两个标签: 职位编号,姓名
XXX-XXX-XXXX-XXX," BLOOM,DANIEL" ,,
希望名称由2个单独列中的名字和姓氏分隔
输出3个单独的标签:
PositionNumber,LastName,FirstName
XXX-XXX-XXXX,BLOOM,DANIEL
import csv
import pandas as pd
s = open('C:\\HR employees\\Listing.csv','r').read()
chars = ('$','%','^','*','JR','II')
for c in chars:
s = ''.join(s.split(c))
out_file = open('myfile_new.csv','w')
out_file.write(s)
out_file.close()
with open('myfile_new.csv','r') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
for row in reader:
print(row)
csvfile = open('myfile2_new.csv','w')
out_file.write
out_file.close()
答案 0 :(得分:0)
假设我有:
$ cat file.csv
PositionNumber, LastName, FirstName
XXX-XXX-XXXX, "BLOOM, DANIEL"
你可以这样做:
with open('/tmp/file.csv') as csvfile:
r=csv.reader(csvfile)
for row in r:
row=[e.strip().strip('"') for e in row]
print(row)
['PositionNumber', 'LastName', 'FirstName']
['XXX-XXX-XXXX', 'BLOOM', 'DANIEL']
csv的默认值是不加引号的。只需使用列表推导中的两个"
语句处理空格和strip
。