python pandas 数据处理str.replace(),替换异常双括号,字符类似字段重新添加双括号
import pandas as pd import csv file = 'd:/raw_data.txt' new_file="d:/new_data.csv" data = pd.read_csv(file,delimiter=',',quoting=csv.QUOTE_NONE,names=['col1','col2','col3','col4']) for column in ['col1','col2','col3']: data[column]=data[column].str.replace('"','') print(data) data.to_csv(new_file,quoting=csv.QUOTE_NONNUMERIC,escapechar="\\",doublequote=False,index=False,header=False)
源文本raw_data.txt:
"abcd123"",345,"helloworld",567
"apple,"pear,"avoc""ado",789
kiwi"","bre"ad","milk",456
生成的新文本 new_data.csv
"abcd123","345","helloworld",567
"apple","pear","avocado",789
"kiwi","bread","milk",456