Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write output with delimiter in python

Tags:

python

I want to save my output as csv file with custom name and delimiter. I tried this code but now works for me.

out = open('out.csv', 'w')
for row in l:
for column in row:
    out.write('%d;' % column)
out.write('\n')
out.close()

Here is my data

100A7E54111FB143    
100D11CF822BBBDB    
1014120EE9CCB1E0    
10276825CD5B4A26    
10364F56076B46B7    
103D1DDAD3064A66    
103F4F66EEB54308    
104310B0280E4F20    
104E80752424B1C3    
106BE9DBB186BEC5    
10756F745D8A4123    
107966C82D8BAD8     

I want to save like this

input.csv
input_id data
number   107966C82D8BAD8 | 10756F745D8A4123 | 106BE9DBB186BEC5

The delmiter would be '|'.The data is in dtype:object

Any help will be appreciated.

like image 936
R.Aarthika Rajendran Avatar asked Jan 30 '16 09:01

R.Aarthika Rajendran


2 Answers

Use a writer for the CSV object instead:

import csv

with open('out.csv', 'w', newline='') as out:
    spamwriter = csv.writer(out, delimiter='|')
    spamwriter.writerow(column)

I have omitted your for loop

like image 73
Moses Koledoye Avatar answered Oct 27 '22 18:10

Moses Koledoye


One simple way is to use print of python 3.x, If you are using python 2.x then import print from future

from __future__ import print_function   #Needed in python 2.x 

print(value, ..., sep=' ', end='\n', file=sys.stdout)
like image 38
AlokThakur Avatar answered Oct 27 '22 17:10

AlokThakur