Search code examples
pythoncsvwriter

Python csv.writer adds unwanted characters


I am letting my script read a csv file and I want to save it first into an array, then slice the second element (which is the second line of my csv file) and write the array back to the csv file. csv file looks like this before:

first name,username,age,status
test1,test1_username,28,2023-08-18 13:41:10+00:00
test2,test2_username,28,2023-08-18 13:41:10+00:00
test3,test3_username,28,2023-08-18 13:41:10+00:00
test4,test4_username,28,2023-08-18 13:41:10+00:00
test5,test5_username,28,2023-08-18 13:41:10+00:00
test6,test6_username,28,2023-08-18 13:41:10+00:00
test7,test7_username,28,2023-08-18 13:41:10+00:00
test8,test8_username,28,2023-08-18 13:41:10+00:00

After running my script, the csv is this:

first name,username,age,status
test2', 'test2_username', '28', '2023-08-18 13:41:10+00:00']]
test3', 'test3_username', '28', '2023-08-18 13:41:10+00:00']]
test4', 'test4_username', '28', '2023-08-18 13:41:10+00:00']]
test5', 'test5_username', '28', '2023-08-18 13:41:10+00:00']]
test6', 'test6_username', '28', '2023-08-18 13:41:10+00:00']]
test7', 'test7_username', '28', '2023-08-18 13:41:10+00:00']]
test8', 'test8_username', '28', '2023-08-18 13:41:10+00:00']]

But this is the output which I want (The same as before but without the second line):

first name,username,age,status
test2,test2_username,28,2023-08-18 13:41:10+00:00
test3,test3_username,28,2023-08-18 13:41:10+00:00
test4,test4_username,28,2023-08-18 13:41:10+00:00
test5,test5_username,28,2023-08-18 13:41:10+00:00
test6,test6_username,28,2023-08-18 13:41:10+00:00
test7,test7_username,28,2023-08-18 13:41:10+00:00
test8,test8_username,28,2023-08-18 13:41:10+00:00

If I run it multiple times then more characters like ' and " are appearing out of nowhere? Why does this happen?

How exactly do I solve this problem? This is my code:

data = []

rows = []
with open('list.csv', 'r', encoding='UTF-8') as f:
    data = csv.reader(f,delimiter=",",lineterminator="\n")
    for row in data:
        rows.append([row,])

rows = rows[2:]
with open('list.csv', 'w', encoding='UTF-8') as g:
    writer = csv.writer(g, delimiter="\n")
    writer2 = csv.writer(g, lineterminator="\n")
    writer2.writerow(['first name', 'username', 'age', 'status'])

Thanks in advance!


Solution

  • You're not writing anything to the output file (or it is missing in example). Also, use writer.writerows to write the data:

    import csv
    
    rows = []
    with open("in.csv", "r", encoding="UTF-8") as f:
        data = csv.reader(f, delimiter=",", lineterminator="\n")
        for row in data:
            rows.append(row)    # <-- append only `row` to the list, not `[row]`
    
    rows = rows[2:]
    with open("out.csv", "w", encoding="UTF-8") as g:
        writer = csv.writer(g, lineterminator="\n")
        writer.writerow(["first name", "username", "age", "status"])
        writer.writerows(rows)  # <-- use `writer.writerows` to write the data in one one step
    

    out.csv will contain:

    first name,username,age,status
    test2,test2_username,28,2023-08-18 13:41:10+00:00
    test3,test3_username,28,2023-08-18 13:41:10+00:00
    test4,test4_username,28,2023-08-18 13:41:10+00:00
    test5,test5_username,28,2023-08-18 13:41:10+00:00
    test6,test6_username,28,2023-08-18 13:41:10+00:00
    test7,test7_username,28,2023-08-18 13:41:10+00:00
    test8,test8_username,28,2023-08-18 13:41:10+00:00