我有一个 csv 文件,文字为
“苹果”
包括重复多次的引号。我需要删除该单词的所有实例以及引号。我该如何在python中做到这一点?
答案 0 :(得分:0)
我已经开发出可以执行您想要的代码。这些评论说明正在做什么。
filename = 'filename.csv' #Put your filename here
word_delete = '"Apple"' #Put what you want to delete here
with open(filename, 'r') as file_text: #Open your file
lines = file_text.readlines() #Read all the lines from your file, then put them in a list
newlines = [] #Make a list to save edits in
for line in lines: #Loop over all the lines, each line will first go into variable line for some actions
newline = line.replace(word_delete, '') #Replace the text in word_delete to an empty string (aka nothing)
newlines.append(newline) #Add the edit to the list with edits
with open(filename, 'w') as file_text: #Open the file, but in write mode
file_text.writelines(newlines) #Write the list with edits to the new file
我希望您现在可以继续进行您的项目!如果有任何其他问题,或者您什么都没收到,我会开放。
问候, CCD的Chris van den Hoorn
编辑:请注意,堆栈溢出不仅仅是问别人如何为您编写代码,我知道您问过如何做到这一点,而是在将来变得更加具体。