Changing all occurences of similar word in csv python
Question: I want to replace one specific word, 'my' with 'your'. But seems my code can
only change one appearance.

    
    
    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
    

let's say the sentences is

    
    
    'my book is gone and my bag is missing'
    

the output is

    
    
    your book is gone and my bag is missing
    

the second thing is I want to make it appear without comma separated:

    
    
    print row
    

the output is

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


Answer: for the second problem, im still trying to find the correct one as it keeps
giving me the same output with comma separated.

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

it gives me the result:

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

I send out this sentence to my Nao robot and the robot seems pronouncing
awkwardly as there are commas in between each word.

I solved it by:

    
    
    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
    

It gives me what I want:

    
    
    your book is gone and your bag is missing too
    

However, any better way to do it?

