Python 2.7 更改csv python中出现的所有相似单词

Python 2.7 更改csv python中出现的所有相似单词,python-2.7,csv,Python 2.7,Csv,我想用“你的”替换一个特定的词“我的”。但我的代码似乎只能改变一种外观 import csv path1 = "/home/bankdata/levelout.csv" path2 = "/home/bankdata/leveloutmodify.csv" in_file = open(path1,"rb") reader = csv.reader(in_file) out_file = open(path2,"wb") writer = csv.writer(out_file) with o

我想用“你的”替换一个特定的词“我的”。但我的代码似乎只能改变一种外观

import csv
path1 = "/home/bankdata/levelout.csv"
path2 = "/home/bankdata/leveloutmodify.csv"
in_file = open(path1,"rb")
reader = csv.reader(in_file)
out_file = open(path2,"wb")
writer = csv.writer(out_file)

with open(path1, 'r') as csv_file:
    csvreader = csv.reader(csv_file)
    col_count = 0
    for row in csvreader:
        while row[col_count] == 'my':
            print 'my is used'
            row[col_count] = 'your'
            #writer.writerow(row[col_count])               
        writer.writerow(row)               
        col_count +=1
让我们假设句子是

'my book is gone and my bag is missing'
输出是

your book is gone and my bag is missing
第二件事是我想让它不以逗号分隔:

print row
输出是

your,book,is,gone,and,my,bag,is,missing,

对于第二个问题,我仍然在努力找到正确的答案,因为它总是给我相同的输出,以逗号分隔

with open(path1) as infile, open(path2, "w") as outfile:
    for row in infile:
        outfile.write(row.replace(",", ""))
print row
结果是:

your,book,is,gone,and,my,bag,is,missing
我把这句话发送给我的Nao机器人,机器人的发音似乎很笨拙,因为每个单词之间都有逗号

我通过以下方式解决了这个问题:

with open(path1) as infile, open(path2, "w") as outfile:
    for row in infile:
       outfile.write(row.replace(",", ""))
with open(path2) as out:
    for row in out:
       print row
它给了我想要的:

your book is gone and your bag is missing too

但是,还有更好的方法吗?

帮助(str.replace)
可能会对您有很大帮助,但您的代码不起作用,因为您增加了
列计数
每一行,所以它实际上不是真正的列计数。您也可以使用
csv.writer
构造函数上的
delimeter
参数,可以看到它使用空格来分隔,而不是逗号。谢谢。我已经解决了用“你的”替换“我的”这个词的问题。我认为你是在试图用最难的方式做简单的事情。尝试简化您的文件解析器以接受句子中的空格。并在文件中使用其他分隔符。。。