1

I have looked through the 'similar questions' here and didn't see one that was close.

I have a text file and headers that I write in Python, but when I open and then write data to the file it starts off immediately after the header, when I want the data to start on the line immediately under the header. I looked at some suggestions here and it said to use the lineterminator argument but that hasn't fixed anything so I think I must be misusing it?

What I try:

import csv

results_file_path = ("C:\\Users\\priper\\Desktop\\freq_sweep_results\\headers_01.txt")



with open(results_file_path, 'w', newline='') as filey: 
    csv_writer = csv.writer(filey, delimiter = '\t', lineterminator='\n')
    csv_writer.writerow(['Freq', 'a1','b1','c1'])
filey.close()

frequency = 1000
a1_results =  [6.2]
b1_results = [9.9]
c1_results = [90.0]

with open(results_file_path, 'a', newline='') as filey: 
    csv_writer = csv.writer(filey, delimiter = '\t', lineterminator='\n')
    csv_writer.writerow([frequency, a1_results[-1], b1_results[-1],  c1_results[-1] ])   

text file results:

Freq    a1  b1  c11000  6.2 9.9 90.0

desired text file results:

Freq    a1  b1  c1
1000    6.2 9.9 90.0

Any ideas?

Windy71
  • 851
  • 1
  • 9
  • 30
  • have you tried with lineterminator='\r\n'? – Cyrus Feb 24 '20 at 15:10
  • 1
    When I run your code I get the desired results. – Bill Bell Feb 24 '20 at 15:13
  • I get the desired results too, both on Python 2 and on Python 3, both with and without `newline=''`. – Thomas Feb 24 '20 at 15:14
  • Cyrus can you post that as an answer, as it works, what is the 'r\ ' for? – Windy71 Feb 24 '20 at 15:14
  • @Windy71 Are you on Windows by any chance, and checking the file with Notepad? Notepad doesn't show Unix line endings (`\n`), only Windows line endings (`\r\n`). – Thomas Feb 24 '20 at 15:15
  • Its odd that it works for some of you, it used to for me, but today it has been putting the data on the same line yet doesn't when I use Cyrus' suggestion. – Windy71 Feb 24 '20 at 15:16
  • You may be onto something there Thomas, i am using Windows and Notepad - unfortunately that is what the engineers I am writing this for will use. Thanks Thomas! – Windy71 Feb 24 '20 at 15:17
  • have a look at this post: https://stackoverflow.com/questions/1761051/difference-between-n-and-r – Cyrus Feb 24 '20 at 15:26
  • I suggest leaving the `lineterminator='\n'` out completely and let the `csv` module deal with it. – martineau Feb 24 '20 at 15:34

1 Answers1

2

Can you try changing this line

csv_writer = csv.writer(filey, delimiter = '\t', lineterminator='\n')

to

csv_writer = csv.writer(filey, delimiter = '\t', lineterminator='\r')

The "lineterminator='\r'" permit to pass to next row,

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • you will need both, as in windows both are needed. https://stackoverflow.com/questions/1761051/difference-between-n-and-r – Cyrus Feb 24 '20 at 15:27
  • Hi Cyrus, I would have happily accepted your answer, you were first and correct, much appreciated the answer and the explanation, thank you! – Windy71 Feb 25 '20 at 07:33