简体   繁体   English

使用Python 3.5从CSV文件中的仅两列中删除特定字符

[英]Delete specific characters from only two columns in a CSV file using Python 3.5

I am trying to edit a CSV file containing 4 million rows of data with 19 columns. 我正在尝试编辑一个包含19列的400万行数据的CSV文件。 There are two columns (the third and fourth) which list names of individuals, and the way their names are listed are "LastName, FirstName." 有两列(第三和第四列),列出了个人的姓名,其姓名的列出方式为“姓,名”。

C00431445,"P80003338","Obama, Barack","DUCLOS, DUNCAN","CHICAGO","IL","606601303","OBAMA FOR AMERICA","ACCOUNTING MANAGER",77.65,08-AUG-08,"","","","SA17A","753821","5433431","P2008",

This is problematic because when I try to upload this file into MySQL using a delimiter of commas, it splits these 2 columns' names in half. 这是有问题的,因为当我尝试使用逗号分隔符将此文件上传到MySQL时,会将这两列的名称分成两半。 I want to use Python 3.5 to select these two columns and remove the commas from inside them only, without deleting the commas in the other rows. 我想使用Python 3.5选择这两列并仅从其中删除逗号,而不删除其他行中的逗号。

I am somewhat of a novice when it comes to coding and any help is appreciated. 在编码方面,我还是一个新手,可以提供任何帮助。 I know it's possible to split these columns using .split() and then merging them sans commas, however I wanted a cleaner method which would remove the commas directly from this file. 我知道可以使用.split()拆分这些列,然后合并它们(不带逗号),但是我想要一种更干净的方法,该方法可以直接从此文件中删除逗号。

Use csv module to read and write 使用csv模块进行读写

import csv

f = open('file.csv', 'rb')
reader = csv.reader(f)
your_list = list(reader)


f = open('file.csv', 'wb')
writer = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)

for line in your_list:
   line[2] = line[2].replace(",","")
   line[3] = line[3].replace(",","")
   writer.writerow(line)

In MySQL, read the columns into @variables, then manipulate them as you store them into the actual columns: 在MySQL中,将列读入@variables,然后在将其存储到实际列中时对其进行操作:

LOAD DATA ...
    (id1, id2, @name1, @name2, ...),
    SET name1 = REPLACE(@name1, ',', ''),
        name2 = REPLACE(@name2, ',', '');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM