-5

I have a python file where I get user inputs for student database and I have written them as a csv file. I couldn't get the links in google to read the csv output and write those csv output to a text file.

Sample Csv output:

Name    Roll no Age Mark1   Mark2
a        1       2    3       4
g        3      54    54      3

import csv

with open('abc.csv','r') as f:
    reader=csv.dictreader(f)
    for row in reader:
        output=('xyz.txt','r')

I know how to read a csv file but I don't know how to write those contents to a text file

anonymous
  • 23
  • 1
  • 2
  • 8
  • 1
    please google your question first – Vadim May 05 '17 at 09:21
  • Possible duplicate of [Python script to read a text file and write into a csv file](http://stackoverflow.com/questions/22794520/python-script-to-read-a-text-file-and-write-into-a-csv-file) – Vadim May 05 '17 at 09:22
  • To all, my question is ow to read the csv output and write it to a text file and not reading text file and writing it to csv – anonymous May 05 '17 at 09:31
  • if you believe your question is unique, then please provide your code. show us what you have already done. – Vadim May 05 '17 at 09:36
  • so, you want to write each row of csv to the text file 'xyz.txt'? – Vadim May 05 '17 at 09:55
  • Yes. Each row in csv file along with the headers needs to be written to text file – anonymous May 05 '17 at 10:00

3 Answers3

3

Use the below code Indentation and comments are inline

  1. Read CSV file
  2. Manipulate Fields
  3. Write to file

Blockquote

import csv

#Open file with "w"(write), the file will create automatically.
file = open("/home/xyz/testfile.txt", "w")
#Open CSV file to read CSV, note: reading and write file should be under "with"
with open('/home/xyz/Desktop/ta_cta.csv') as csvFile:
    #Read CSV
    readCsv = csv.reader(csvFile)
    for row in readCsv:
        #Get Values and manupilate in the file.write
        Id = row[0]
        Id1 = row[1]
        #Write CSV you need format it to string if the value is an int
        file.write("UPDATE Schema.TableName SET Column1="+str(Id1)+" where Column2="+str(Id)+";\n")
#You Must Close the FIle after writing it.
file.close()
Sachin Sridhar
  • 434
  • 4
  • 13
0

You should learn python csv module firstly.

https://docs.python.org/2/library/csv.html

0

you can try this code:

import csv

output=open('xyz.txt','w')

with open('abc.csv',"rt", encoding='ascii') as f:
for row in f:
    output.write(row)
Vadim
  • 4,219
  • 1
  • 29
  • 44
  • For loop needs to be intended. Ascii encoding wasnt needed for my file. Please edit them. – anonymous May 05 '17 at 10:11
  • Can you explain why you gave mode as rt and ascii encoding? I can understand the rest of codes. Sorry I'm a beginner in python – anonymous May 05 '17 at 10:12
  • sure, but I bet you can google things, the answer to your question is here:http://stackoverflow.com/questions/23051062/open-files-in-rt-and-wt-modes – Vadim May 05 '17 at 10:15
  • About, ascii encoding, just in case, maybe you will need it. – Vadim May 05 '17 at 10:16
  • Thanks :) How to get the contents of entire row in csv file when i select a particular roll no? – anonymous May 05 '17 at 10:27
  • use the number of the row, or the value. But for the row number solution you will need a row counter. – Vadim May 05 '17 at 10:33
  • can you explain it with a sample code considering rollno 1 or 2 from input ? – anonymous May 05 '17 at 10:35
  • http://stackoverflow.com/questions/12755587/using-python-to-write-specific-lines-from-one-file-to-another-file check this link – Vadim May 05 '17 at 10:54