python csv replace listitem
Question: i have following output from a csv file:

    
    
    word1|word2|word3|word4|word5|word6|01:12|word8
    word1|word2|word3|word4|word5|word6|03:12|word8
    word1|word2|word3|word4|word5|word6|01:12|word8
    

what i need to do is change the time string like this 00:01:12. my idea is to
extract the list item [7] and add a "00:" as string to the front.

    
    
    import csv
    
    with open('temp', 'r') as f:
        reader = csv.reader(f, delimiter="|")
        for row in reader:      
            fixed_time = (str("00:") + row[7])
            begin = row[:6]
            end = row[:8]
            print begin + fixed_time +end
    

get error message:

TypeError: can only concatenate list (not "str") to list.

i also had a look on this post.

[how to change [1,2,3,4] to '1234' using
python](http://stackoverflow.com/questions/2597932/how-to-
change-1-2-3-4-to-1234-using-python/2597937#2597937)

i neeed to know if my approach to soloution is the right way. maybe need to
use split or anything else for this.

thx for any help


Answer: The line that's throwing the exception is

    
    
    print begin + fixed_time +end
    

because `begin` and `end` are **both lists** and `fixed_time` is a string.
Whenever you take a **slice** of a list (that's the `row[:6]` and `row[:8]`
parts), a list is returned. If you just want to print it out, you can do

    
    
    print begin, fixed_time, end
    

and you won't get an error.

### Corrected code:

I'm opening a new file for writing (I'm calling it 'final', but you can call
it whatever you want), and I'm just writing everything to it with the one
modification. It's easiest to just change the one element of the list that has
the line (`row[6] here`), and use `'|'.join` to write a pipe character between
each column.

    
    
    import csv
    
    with open('temp', 'r') as f, open('final', 'w') as fw:
        reader = csv.reader(f, delimiter="|")
        for row in reader:      
            # just change the element in the row to have the extra zeros
            row[6] = '00:' + row[6]
            # 'write the row back out, separated by | characters, and a new line.
            fw.write('|'.join(row) + '\n')
    

