我是python的新手。如何将字符串字符,"
替换为,{
,将",
替换为包含.csv文件中多线的},
?
以下是.csv文件的内容
Name, Degree,Some, Occupation, Object
Mr. A,"B.A, M.A",123,"ags,gshs",ass
Mr. ABC,"B.A, M.A",231,"ags,gshs",asas
Mr. D,"BB.A, M.A",44,"ags,gshs",asas
Mr. G,"BBBB.A, M.A",12,"ags,gshs",asasasa
Mr. S,"B.A, MMM.A",10,"ags,gshs",asasas
Mr. R,"B.A, M.A",11,"ags,gshs",asasas
Mr. T,"B.A, M.A",12,"ags,gshs",asasa
Mr. DD,"B.A, M.A",13,"ags,gshs",asasas
所以我的输出将是这样的
Name, Degree,Some, Occupation, Obejct
Mr. A,{B.A, M.A},123,{ags,gshs},ass
Mr. ABC,{B.A, M.A},231,{ags,gshs},asas
Mr. D,{BB.A, M.A},44,{ags,gshs},asas
Mr. G,{BBBB.A, M.A},12,{ags,gshs},asasasa
Mr. S,{B.A, MMM.A},10,{ags,gshs},asasas
Mr. R,{B.A, M.A},11,{ags,gshs},asasas
Mr. T,{B.A, M.A},12,{ags,gshs},asasa
Mr. DD,{B.A, M.A},13,{ags,gshs},asasas
答案 0 :(得分:1)
使用file.read()
打开文件后,您可以使用replace(old, new)
替换所需的字符串字符。请注意,由于字符串,"
和",
包含引号,因此您必须在引号前加上\
,以显示它们是字符串的一部分。
编辑:评论提到您可以将字符串括在' '
中。如果您这样做,则不需要在引号之前放置\
。例如,",\""
和',"'
都是有效的字符串。
data = ""
with open("/path/to/file.csv") as file:
data = file.read().replace(",\"", ",{").replace("\",", "},")
with open("/path/to/new_file.csv") as file:
file.write(data)
答案 1 :(得分:0)
如果你只需要它就可以使用这样的熊猫:
import pandas as pd
data1 = '''\
Name,Degree,Some,Occupation,Object
Mr. A,"B.A, M.A",123,"ags,gshs",ass
Mr. ABC,"B.A, M.A",231,"ags,gshs",asas
Mr. D,"BB.A, M.A",44,"ags,gshs",asas
Mr. G,"BBBB.A, M.A",12,"ags,gshs",asasasa
Mr. S,"B.A, MMM.A",10,"ags,gshs",asasas
Mr. R,"B.A, M.A",11,"ags,gshs",asasas
Mr. T,"B.A, M.A",12,"ags,gshs",asasa
Mr. DD,"B.A, M.A",13,"ags,gshs",asasas'''
df = pd.read_csv(pd.compat.StringIO(data1), sep=',', dtype=object)
#df = pd.read_csv('input.csv', sep=',', dtype=object) # Use this row for real application
df['Degree'] = '{'+df['Degree']+'}'
df['Occupation'] = '{'+df['Occupation']+'}'
# Create custom output
out = '\n'.join([','.join(df.columns), '\n'.join(','.join(i) for i in df.values)])
with open('output.csv') as f:
f.write(out)
答案 2 :(得分:0)
您可以使用解包:
import csv
with open('filename.csv') as f:
data = filter(None, list(csv.reader(f)))
with open('filename.csv', 'w') as f1:
write = csv.writer(f1)
write.writerows([data[0]]+[[a, '{'+b+'}', c, '{'+d+'}', e] for a, b, c, d, e in data[1:]])
输出:
Name, Degree,Some, Occupation, Object
Mr. A,{B.A, M.A},123,{ags,gshs},ass
Mr. ABC,{B.A, M.A},231,{ags,gshs},asas
Mr. D,{BB.A, M.A},44,{ags,gshs},asas
Mr. G,{BBBB.A, M.A},12,{ags,gshs},asasasa
Mr. S,{B.A, MMM.A},10,{ags,gshs},asasas
Mr. R,{B.A, M.A},11,{ags,gshs},asasas
Mr. T,{B.A, M.A},12,{ags,gshs},asasa
Mr. DD,{B.A, M.A},13,{ags,gshs},asasas
答案 3 :(得分:0)
试试:
def find_replace(csv_path, search_characters, replace_with):
text = open(csv_path, "r")
text = ''.join([i for i in text]).replace(
search_characters, replace_with)
x = open(csv_path, "w")
x.writelines(text)
x.close()
if __name__ == '__main__':
csv_path = "path/to/csv/file.csv"
search_characters = ',"'
replace_with = ',{'
find_replace(csv_path, search_characters, replace_with)
search_characters = '",'
replace_with = '},'
find_replace(csv_path, search_characters, replace_with)
以上代码打开文件,向其中写入一些数据,然后关闭它。
或者,如果您更喜欢 list
以及 with
语句,它会注意调用给定对象的 __exit__
函数,即使代码中发生了不好的事情。
def find_replace(csv_path, search_characters, replace_with):
s_one, s_two = search_characters
r_one, r_two = replace_with
with open(csv_path) as file:
data = file.read().replace(s_one, r_one).replace(s_two, r_two)
with open(csv_path, 'w') as file:
file.write(data)
if __name__ == '__main__':
csv_path = "path/to/csv/file.csv"
search_characters = [',"', '",']
replace_with = [',{', '},']
find_replace(csv_path, search_characters, replace_with)
使用 with
语句的主要优点是它可以确保我们的文件关闭,而无需注意嵌套块的退出方式。
经过测试并在您的示例上运行良好。