A
A
Alexander Mishchenko2020-01-26 15:42:14
Python
Alexander Mishchenko, 2020-01-26 15:42:14

How to add an empty element to the end when writing to csv?

There is a function that saves data in csv

def csv_writer(data, path):
    with open(path, 'a', newline='', encoding='utf-8') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL)
        spamwriter.writerow(data)

Everything is processed correctly. But when uploading a file to the site, the site parser does not see the last cell. If you open the file in Notepad, you can see that all cells are separated by ";" (semicolon), and the last element does not have a semicolon. How to add a semicolon to the last cell

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2020-01-26
@xxxsanchosxxx

If we're talking about extra. separator after the last value, then you can use extra. parameter or artificially add an empty column by changing the linelineterminator=";\r\n"
spamwriter.writerow(data + [None])

A
Alexander Mishchenko, 2020-01-27
@xxxsanchosxxx

Added an additional parameter to the function and everything turned out lineterminator=";\r\n"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question