1

I have my 500000 numerical values txt file.Couple of lines

  -938.549927    
  -938.542419    
  -938.534912    
  -938.523621    
  -938.523621    
  -938.512329    
  -938.512329    
  -938.523621    
  -938.519836    
  -938.523621    
  -938.519836    
  -938.508606    
  -938.508606    
  -938.508606    
  -938.519836    
  -938.531128    
  -938.538635   

I want so save it as csv file.I have tried this

import csv

lines = [line.rstrip('\n') for line in open('d500.txt')]

myfile = open('n500.csv', 'wb')
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(lines)

I got this

wc -l n500.csv
1 n500.csv

Just part of the file

358337    ","  -938.362061    ","  -938.369568    ","  -938.369568    ","  -938.369568    ","  -938.380859    ","  -938.377075    ","  -938.380859    ","  -938.373352    ","  -938.347046    ","  -938.343262    ","  -938.339539    ","  -938.324524    ","  -938.313232    ","  -938.328247    ","  -938.320740    ","  -938.320740    ","  -938.328247    ","  -938.324524    ","  -938.324524    ","  -938.316956  

What I really want is something like this

 -938.316956   
 -938.316956   
 -938.313232  

If I try

wr.writerows(lines)

format is crazy

" "," ","-","9","3","8",".","3","1","3","2","3","2"," "," "," "," "
" "," ","-","9","3","8",".","3","2","8","2","4","7"," "," "," "," "
" "," ","-","9","3","8",".","3","2","0","7","4","0"," "," "," "," "
" "," ","-","9","3","8",".","3","2","0","7","4","0"," "," "," "," "
" "," ","-","9","3","8",".","3","2","8","2","4","7"," "," "," "," "
" "," ","-","9","3","8",".","3","2","4","5","2","4"," "," "," "," "
" "," ","-","9","3","8",".","3","2","4","5","2","4"," "," "," "," "
" "," ","-","9","3","8",".","3","1","6","9","5","6"," "," "," "," "
" "," ","-","9","3","8",".","3","1","3","2","3","2"," "," "," "," "
" "," ","-","9","3","8",".","3","1","3","2","3","2"," "," "," "," "
" "," ","-","9","3","8",".","3","0","9","4","4","8"," "," "," "," "
" "," ","-","9","3","8",".","3","1","6","9","5","6"," "," "," "," "
" "," ","-","9","3","8",".","3","1","6","9","5","6"," "," "," "," "
" "," ","-","9","3","8",".","3","1","3","2","3","2"," "," "," "," "

My new code version

import csv

lines = [[line.rstrip('\n')] for line in open('d500.txt')]

myfile = open('n500.csv', 'wb')
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerows(lines)

Than I got

"  -938.324524    "
"  -938.313232    "
"  -938.328247    "
"  -938.320740    "
"  -938.320740    "
"  -938.328247    "
"  -938.324524    "
"  -938.324524    "
"  -938.316956    "
"  -938.313232    "
"  -938.313232    "
"  -938.309448    "
"  -938.316956    "
"  -938.316956    "
"  -938.313232    "

How to get rid of quotation marks?

16
  • You have failed to show what d500.txt looks like. Commented Dec 27, 2016 at 18:07
  • @IgnacioVazquez-Abrams Please take a look now! Commented Dec 27, 2016 at 18:08
  • writerow does as the name says, it writes a row. So your list of values is interpreted as one row. Commented Dec 27, 2016 at 18:10
  • @ppasler So what should I do? Commented Dec 27, 2016 at 18:12
  • use writerows instead Commented Dec 27, 2016 at 18:12

2 Answers 2

2

All you have to do is rename your .txt to .csv (or copy to a .csv file).

Since there is only one column, no (comma) seperators are needed.

Sign up to request clarification or add additional context in comments.

3 Comments

Since OP is working on Ubuntu and Windows the line breaks matter.
@ppasler They really shouldn't matter. Any decent parser should be able to handle both forms. If not, all open data on the net would need to have two versions (some are with crlf, others with lf only).
indeed it shouldn't matter, just thought, as he uses rstrip explicitly to remove line breaks.
2

Use writeline inside the loop

import csv

with open('n500.csv', 'wb') as myfile:
    wr = csv.writer(myfile)
    [wr.writerow(line.rstrip('\n')) for line in open('d500.txt')]

The with statement handle file closing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.