- 1
[Python] Code spawns empty lines when writing to a csv file
Hey there. I wrote this stats collection tool for mastodon today, but unfortunately something is wrong with the csv part of it. Whenever it appends a line to a file it spawns an additional empty line. Can anyone help? Here's the code itself: https://code.sololearn.com/c8wYTEZBsIIx/#py
17 Answers
0
guess i've found a solution.
https://stackoverflow.com/questions/3348460/csv-file-written-with-JUMP_LINK__&&__python__&&__JUMP_LINK-has-blank-lines-between-each-row#3348664
I feel like an idiot now but it worked. anyway thanks for the help
+ 1
That's the normal behavior: each row (line) of data end with a new line character, and the final row doesn't make exception ^^
Think at this last empty line differently: a new line doesn't start with a new line char (wich is expected to mark a line end), and this last empty line is not really one (it's juste the visually rendering of the end line -- where cursor could be positionned at start of a potentially new line, as soon as you type in or append some text)...
+ 1
Without your source file data, it's hard to really run your code and so investigate further...
Could you provide the data, or at least a sample of it? (if the request necessarly to run your code could be done publicly -- else we need at least a sample of result of some valid requests)
I've not successed to reproduce your bug in my tests...
You could try to read and print the output file (csv) with the repr() method to check if that was not related with line end encoding (unix: LF, max: CR; dos: CR/LF)... maybe you write CR/LF as new line chars and you open the csv file in bad mode wich will interpret CR/LF as two new lines chars ?
+ 1
in the zip you've provided, there's a measurements.csv file: is it the output were you have the bug? because when I open it in a text editor, it would display empty lines between data lines... If so, I guess my last suggestion was right and you've facing a problem related to end line encoding (in the software used to open the csv text - try another editor at least if you don't want to print(repr(csv)) test) ^^
0
visph I see what you mean, but still couldn't come up with a solution. Can you tell me how exactly should I change the code to fix this issue?
0
From my point of view that's not an issue: if you 'fix' this behavior, then on next file append you will write on same line than previous (and as you append row after row, you will inlined all your row, without separator). Until you write a new line each time you want to write a row (and the 'fix' must do almost the inverse: after a row write, you need to delete the last new line char -- copy all the file minus the last char...) ^^
0
You didn't get it. The output file looks like this right now:
Info info info
Info info info
Info info info
...
But it's supposed to be like that instead:
Info info info
Info info info
Info info info
Info info info
0
It writes on an every other line instead of an each line visph
0
visph I can provide you with source data, but not in a public forum. Can we get in touch with telegram, signal or email?
0
Is it possible to provide fake data? (those wch will be fetch by the request) for testing purpose?
Anyway, I doubt I could reproduce your bug, but I could try... Have you attempted my last suggestion of outputing the csv content with print(repr(text))?
0
Not yet. Will Google it though (I'm just a beginner unfortunately). I will provide you with demo data for reproduction soon, working on it now. visph
0
visph here are the files: https://1drv.ms/u/s!Alhi8U4eMypSnUxdmYiHLBXcm3j5?e=9Q6fnW
The file paths must be changed manually in order to run on your machine. Sorry for that, i'm still figuring that part out.
0
I will have to Google it because I've never heard of this operator and don't know what it does, but thanks
0
the output of the line print(repr(open(r"C:\Python\Mastodon\Follower-count\measurements.csv", "r"))) is following:
<_io.TextIOWrapper name='C:\\Python\\Mastodon\\Follower-count\\measurements.csv' mode='a' encoding='cp1251'>
I've also tried opening the file in another text editor - shows the same
i also can' just remove the last char from the line that is being appended because the "new line" char is added by the csv module and not me. I'm stuck.
0
Wow!
So you're using python2?
That's an important information to share when you request help ^^
Anyway, the output I was suggested to do, was the output of the file CONTENT (print(repr(open(path').read())) if you really want to inline it)... and you would have seen the \r\r\n sequence (CR/CR/LF in other words) in something such as:
'date;username;followers;posts\r\r\n2020-04-02 19:02;...'
0
No, I'm using python 3. I just didn't know about the newline='' thing
0
Oh... I was guessing you're used Python2 because the stackoverflow post seems to say that's a problem related to Python2 (but I don't have read the post entirely, and absolutly not all the thread :P)