Python csv.writer adds unwanted characters

55 Views Asked by At

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!

3

There are 3 best solutions below

0
Andrej Kesely On BEST ANSWER

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
0
Barmar On

You're getting the brackets and quotes because you're wrapping each row in another list. Rows should be 1-dimensional lists, not 2-dimensional.

If you want to remove a row from the CSV, you can simplify it by just deleting that element from rows, then writing all the rows back to the file.

with open('list.csv', 'r', encoding='UTF-8') as f:
    rows = list(csv.reader(f,delimiter=",",lineterminator="\n"))

del rows[1]

with open('list.csv', 'w', encoding='UTF-8') as g:
    csv.writer(g,delimiter=",",lineterminator="\n").writerows(rows)
1
amchugh89 On

Less lines with pandas

import pandas as pd

df = pd.read_csv('in.csv')
df = df.drop([0]) #drop row
out = df.to_csv('out.csv', index = False)