Python 3:

with open('data.csv', 'a', newline='') as fp:
    for player in self.players:
        a = csv.writer(fp, delimiter=',');
        data = [[player.name, player.penalty(), player.score()]];
        a.writerows(data);

With python 3 there is change in the CSV module you can read here

Python 2.x:

Just change the open() to binary open('data.csv', 'ab')

You can set control quoting using:

csv.writer(fp, delimiter=',',quoting=csv.QUOTE_MINIMAL)

As from docs your options are:

csv.QUOTE_ALL Instructs writer objects to quote all fields.

csv.QUOTE_MINIMAL Instructs writer objects to only quote those fields which contain special characters such as delimiter, quotechar or any of the characters in lineterminator.

csv.QUOTE_NONNUMERIC Instructs writer objects to quote all non-numeric fields.

Instructs the reader to convert all non-quoted fields to type float.

csv.QUOTE_NONE Instructs writer objects to never quote fields. When the current delimiter occurs in output data it is preceded by the current escapechar character. If escapechar is not set, the writer will raise Error if any characters that require escaping are encountered.

Answer from Kobi K on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ csv writer: append doesn't go to new line
r/learnpython on Reddit: csv writer: append doesn't go to new line
January 26, 2021 -

So, basically, I've got this code:

new_list_csv = []

def add_it():
    #gets the values from Entry
    #three different tk Entrys generate three different values
    name= self.e.get()
    ex= self.e_x.get()
    ey= self.e_y.get()
    #adds them to the new list
    new_list_csv.append(name)
    new_list_csv.append(ex)
    new_list_csv.append(ey)
    #appends new characters to csv file
    with open ('chr_list_copy.txt', 'a', newline='') as write_obj:   
        csv_writer = csv.writer(write_obj)
        csv_writer.writerow(new_list_csv)

and the csv file looks like this:

name,x,y
Ergo,1,1
Sum,5,5
Name12,1,2
Name34,3,4
Name56,5,6
Name78,7,8
Name910,9,10
Name1112,11,12
Name1314,13,14

if I try and add [Otto,6,9], I get:

name,x,y
[...]
Name1314,13,14Otto,6,9

instead of:

name,x,y
[...]
Name1314,13,14
Otto,6,9

I've used this very same structure for the rest of my code, but in this specific instance, it doesn't work.

Discussions

How to append a new row to an old CSV file in Python? - Stack Overflow
Adding the newline='', as Natacha ... between Python 2 and 3. ... Save this answer. ... Show activity on this post. Based in the answer of @G M and paying attention to the @John La Rooy's warning, I was able to append a new row opening the file in 'a'mode. Even in windows, in order to avoid the newline problem, you must declare it as newline=''. Now you can open the file in 'a'mode (without the b). import csv with ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
CSV in Python adding an extra carriage return, on Windows - Stack Overflow
With Python 3.5.2, this was the only thing that worked for me (well, I used just lineterminator='\n'); the CSV module seemed to be the origin of \r\n. No set of arguments to open had any effect. 2019-01-24T19:30:13.43Z+00:00 ... Save this answer. ... Show activity on this post. You have to add attribute newline... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - to_csv append mode is not appending to next new line - Stack Overflow
I am guessing using a CSV writer instead of to_csv but clearly the append mode is not skipping the last line of the existing file. ... Are you using the pandas package? You do not mention that anywhere. Pandas does not automatically append a new line, and I am not sure how to force it. More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 18, 2016
python - data not appending to new line. getting inserted into last line and creating new columns - Stack Overflow
I have written some code in python that is used to append rows into a csv file. However, my code enters it into the same line, creating new columns. I want to append the updated list into my csv f... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GitHub
github.com โ€บ python โ€บ cpython โ€บ issues โ€บ 100821
csv: Doesn't append a new row with writerow() ยท Issue #100821 ยท python/cpython
January 7, 2023 - Bug report When I want to add a new row to an existing file it doesn't append a new row. But it extends the last row. def ajout_csv(nom, ordre, lignes): with open(nom + '.csv', mode='a', newline='') as csv_file: writer = csv.DictWriter(c...
Author ย  python
๐ŸŒ
Quora
quora.com โ€บ How-do-I-insert-a-new-line-when-adding-to-a-CSV-file-in-Python
How to insert a new line when adding to a CSV file in Python - Quora
Answer: [code]import csv # Open the CSV file in append mode with open('file.csv', 'a', newline='') as csvfile: # Create a CSV writer object writer = csv.writer(csvfile) # Write the new line to the CSV file writer.writerow(['value1', 'value2', 'value3']) [/code]I hope this was he...
Top answer
1 of 6
540

Python 3:

The official csv documentation recommends opening the file with newline='' on all platforms to disable universal newlines translation:

with open('output.csv', 'w', newline='', encoding='utf-8') as f:
    writer = csv.writer(f)
    ...

The CSV writer terminates each line with the lineterminator of the dialect, which is '\r\n' for the default excel dialect on all platforms because that's what RFC 4180 recommends.


Python 2:

On Windows, always open your files in binary mode ("rb" or "wb"), before passing them to csv.reader or csv.writer.

Although the file is a text file, CSV is regarded a binary format by the libraries involved, with \r\n separating records. If that separator is written in text mode, the Python runtime replaces the \n with \r\n, hence the \r\r\n observed in the file.

See this previous answer.

2 of 6
291

While @john-machin gives a good answer, it's not always the best approach. For example, it doesn't work on Python 3 unless you encode all of your inputs to the CSV writer. Also, it doesn't address the issue if the script wants to use sys.stdout as the stream.

I suggest instead setting the 'lineterminator' attribute when creating the writer:

import csv
import sys

doc = csv.writer(sys.stdout, lineterminator='\n')
doc.writerow('abc')
doc.writerow(range(3))

That example will work on Python 2 and Python 3 and won't produce the unwanted newline characters. Note, however, that it may produce undesirable newlines (omitting the LF character on Unix operating systems).

In most cases, however, I believe that behavior is preferable and more natural than treating all CSV as a binary format. I provide this answer as an alternative for your consideration.

Find elsewhere
Top answer
1 of 3
10

With some tinkering I realized you can add the following line to make sure you begin writing on a new line in a csv. Though it seems kind of hackish. Documentation mentions a lot about a kwarg newline='', but it wasn't recognized as valid.

writer.writerow([])

I also open with 'ab' parameter.

import csv
with open('mycsvfile.csv','ab') as f:
    writer=csv.writer(f)
    writer.writerow([])
    writer.writerow(['0','0','0'])
2 of 3
9

The problem is your original file didn't have a final newline written to it. this reproduces the problem:

#!python3
import csv

#initial content
with open('mycsvfile.csv','w') as f:
    f.write('a,b,c\n1,1,1') # NO TRAILING NEWLINE

with open('mycsvfile.csv','a',newline='') as f:
    writer=csv.writer(f)
    writer.writerow([0,0,0])
    writer.writerow([0,0,0])
    writer.writerow([0,0,0])

with open('mycsvfile.csv') as f:
    print(f.read())

Output:

a,b,c
1,1,10,0,0
0,0,0
0,0,0

Just make sure the original file was generated properly:

#!python3
import csv

#initial content
with open('mycsvfile.csv','w') as f:
    f.write('a,b,c\n1,1,1\n') # TRAILING NEWLINE

with open('mycsvfile.csv','a',newline='') as f:
    writer=csv.writer(f)
    writer.writerow([0,0,0])
    writer.writerow([0,0,0])
    writer.writerow([0,0,0])

with open('mycsvfile.csv') as f:
    print(f.read())

Output:

a,b,c
1,1,1
0,0,0
0,0,0
0,0,0

You can do some hack to seek to the end of the file and decide to write the extra newline, but better to fix the existing file generation so it always writes newlines. The easiest way to do that is use the csv module from the start, since it will always add a newline with writerow.

Top answer
1 of 11
1444

The csv.writer module directly controls line endings and writes \r\n into the file directly. In Python 3 the file must be opened in untranslated text mode with the parameters 'w', newline='' (empty string) or it will write \r\r\n on Windows, where the default text mode will translate each \n into \r\n.

#!python3
with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
    writer = csv.writer(outfile)

If using the Path module:

from pathlib import Path
import csv

with Path('/pythonwork/thefile_subset11.csv').open('w', newline='') as outfile:
    writer = csv.writer(outfile)

If using the StringIO module to build an in-memory result, the result string will contain the translated line terminator:

from io import StringIO
import csv

s = StringIO()
writer = csv.writer(s)
writer.writerow([1,2,3])
print(repr(s.getvalue()))  # '1,2,3\r\n'   (Windows result)

If writing that string to a file later, remember to use newline='':

# built-in open()
with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as f:
    f.write(s.getvalue())

# Path's open()
with Path('/pythonwork/thefile_subset11.csv').open('w', newline='') as f:
    f.write(s.getvalue())

# Path's write_text() added the newline parameter to Python 3.10.
Path('/pythonwork/thefile_subset11.csv').write_text(s.getvalue(), newline='')

In Python 2, use binary mode to open outfile with mode 'wb' instead of 'w' to prevent Windows newline translation. Python 2 also has problems with Unicode and requires other workarounds to write non-ASCII text. See the Python 2 link below and the UnicodeReader and UnicodeWriter examples at the end of the page if you have to deal with writing Unicode strings to CSVs on Python 2, or look into the 3rd party unicodecsv module:

#!python2
with open('/pythonwork/thefile_subset11.csv', 'wb') as outfile:
    writer = csv.writer(outfile)

Documentation Links

  • https://docs.python.org/3/library/csv.html#csv.writer
  • https://docs.python.org/2/library/csv.html#csv.writer
2 of 11
93

Opening the file in binary mode "wb" will not work in Python 3+. Or rather, you'd have to convert your data to binary before writing it. That's just a hassle.

Instead, you should keep it in text mode, but override the newline as empty. Like so:

with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
๐ŸŒ
Stack Exchange
datascience.stackexchange.com โ€บ questions โ€บ 24670 โ€บ python-inserts-newline-by-writing-to-csv
Python inserts newline by writing to csv - Data Science Stack Exchange
all_tr = soup.find_all("tr") for movie in range(1, len(all_tr)): row=[] counter+=1 row.append(counter) td = all_tr[movie].find_all("td") for colIndex in range(1, len(td)): row.append(td[colIndex].string) writer.writerow(row) ... ID|Release Date|Movie|Production Budget|Domestic Gross|Worldwide Gross 1|12/18/2009|Avatar|$425,000,000|$760,507,625|$2,783,918,982 2 3|5/20/2011|Pirates of the Caribbean: On Stranger Tides|$410,600,000|$241,063,875|$1,045,663,875 4 5|5/1/2015|Avengers: Age of Ultron|$330,600,000|$459,005,868|$1,408,218,722 6 ยท and so on, with additional counter lines between the output that I don't want, how can I get rid of that?
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-append-a-new-row-to-an-existing-csv-file
How to append a new row to an existing csv file? - GeeksforGeeks
October 13, 2022 - To achieve this, we can utilize the to_csv() function in Pandas with the 'a' parameter to write the DataFrame to the CSV file in append mo ... Working with CSV files is a common task in data manipulation and analysis, and Python provides versatile ...
๐ŸŒ
Linux Hint
linuxhint.com โ€บ append-new-row-csv-python
Linux Hint โ€“ Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to remove '\r\n' from the csv records being written?
r/learnpython on Reddit: How to remove '\r\n' from the csv records being written?
July 30, 2015 -

I am running the script on a Windows 7 machine using Python 3.4.1. The script runs correctly and produces a csv file, the only problem is at the end of each line is a '\r\n' which causes an extra blank line to appear when displayed in Excel. How do get remove the extra blank line?

import pyodbc
import csv

connect = pyodbc.connect('driver={SQL Server Native       Client 10.0};SERVER=xxxxx-7-   VM;DATABASE=DEMO_xxxx_TEST;UID=DEMO_xxxx_TEST;PWD=password')

cursor = connect.cursor()

cursor.execute('''select Report_name
                    ,UserID
                    ,FieldName
                    ,XPos
                    ,YPos
                    ,Hidden
                    ,Picture
              from  Rpt_Nudge_2
              where Report_Name = 'UB04' and UserID = 'myid' and YPos > 8886 and YPos < 9888
              order by YPos, XPos''')

col_names = [i[0] for i in cursor.description]
print(col_names)

nudge = cursor.fetchall()

with open('UB04_nudge.csv', 'w') as csvfile:
    fileout = csv.writer(csvfile)
    row = fileout.writerow(col_names)
    for line in nudge:
        fileout.writerow(line)

connect.close()
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ appending data in csv file
r/learnpython on Reddit: appending data in csv file
September 8, 2021 -

hey, everyone good day! (I'm very new to this coding stuff)

I was writing a simple program and this program needs to append new data from users to the existing csv file so I wrote this function.

def append_list_as_row(file_name, list_of_elem):
    with open(file_name, 'a+', newline='') as write_obj:
        csv_writer = writer(write_obj)
        csv_writer.writerow(list_of_elem)

this just works fine until the last element from the csv file is 0.

let's say we have [Robert][19][male][173] this kind of data in excel. and this works just fine with the function. but when it's like [Robert][19][male][0], the next appended data will not generate a new row and will continue adding data in the current row and replacing 0 to the first data element from the user.

I hope you guys understand my English...welp, anyways I want this function to work seemly whether the last element is 0 or not. I've been searching the internet for quite a long but I was not able to find the answer. is there any kind soul who can help me?

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 73664736 โ€บ append-to-a-csv-file-which-does-not-end-with-newline
python - Append to a CSV file which does not end with newline - Stack Overflow
September 9, 2022 - I would suggest that you look at ... to it), add '\n' in front of it, and then append this string to a text csv file. ... Another option: first add '\n' to the text csv file and then write your string to this file....