0

I want to write data in each row in csv file without overwriting. With this code I am able to write the data in every second row of the file. Please any help.This is the part of the code Im using these code

.... 
dfl_origin= [dfl_origin_X,dfl_origin_Y]
import csv
c = csv.writer(open("C:/Config/Output/box1.csv","a"))
c.writerow([dfl_origin[0],dfl_origin[1]])
....

And the output is these output

So . Why the second data is not written into second row? Any help?

Thanks

bob
  • 363
  • 3
  • 8
  • 21
  • Did you look at contents of `box1.csv` in a text editor, such as Nodepad or Sublime Text? It probably contains a blank line at the bottom of file. If you're using Unix/Linux systems, what's the output of `wc -l box1.csv` without running this script? – Damodar Dahal Jan 11 '18 at 04:47
  • Im not using Unix/Linux system. Working on Windows. When open with Excel second row is empty. But should not be like that, I saw other python data scv file open with Excel and each raw is full filled with data. Something in the code is missing. But can not find what – bob Jan 11 '18 at 05:09
  • 1
    This seems like a line endings issues Windows vs. Unix. Take a look at [this post](https://stackoverflow.com/questions/3191528/csv-in-python-adding-an-extra-carriage-return). tl;dr: try opening the file in binary mode: `'ab'` – pault Jan 11 '18 at 05:17
  • 2
    Try using file mode `ab` instead of just `a`. – Damodar Dahal Jan 11 '18 at 05:21
  • 'ab' doesn't help. Its the same issue – bob Jan 11 '18 at 06:19
  • Can you please provide a [mcve]? Show us some sample data from your dataframes. – pault Jan 11 '18 at 13:08
  • Also did you try the second answer on the page I linked? `c = csv.writer(open("C:/Config/Output/box1.csv","ab"), lineterminator='\n')` – pault Jan 11 '18 at 14:53
  • 1
    the second answer c = csv.writer(open("C:/Config/Output/box1.csv","ab"), lineterminator='\n' works – bob Jan 12 '18 at 07:06
  • Which version of Python are you using? – Martin Evans Jan 15 '18 at 14:56

0 Answers0