Home:ALL Converter>Read output of a csv file and write it to a text file using Python

Read output of a csv file and write it to a text file using Python

Ask Time:2017-05-05T17:20:27         Author:anonymous

Json Formatter

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

Author:anonymous,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43801152/read-output-of-a-csv-file-and-write-it-to-a-text-file-using-python
Sachin Sridhar :

Use the below code Indentation and comments are inline\n\n\nRead CSV file\nManipulate Fields\nWrite to file\n\n\n\n Blockquote\n\n\nimport csv\n\n#Open file with \"w\"(write), the file will create automatically.\nfile = open(\"/home/xyz/testfile.txt\", \"w\")\n#Open CSV file to read CSV, note: reading and write file should be under \"with\"\nwith open('/home/xyz/Desktop/ta_cta.csv') as csvFile:\n #Read CSV\n readCsv = csv.reader(csvFile)\n for row in readCsv:\n #Get Values and manupilate in the file.write\n Id = row[0]\n Id1 = row[1]\n #Write CSV you need format it to string if the value is an int\n file.write(\"UPDATE Schema.TableName SET Column1=\"+str(Id1)+\" where Column2=\"+str(Id)+\";\\n\")\n#You Must Close the FIle after writing it.\nfile.close()\n",
2018-03-29T05:21:57
Vadim :

you can try this code:\n\nimport csv\n\noutput=open('xyz.txt','w')\n\nwith open('abc.csv',\"rt\", encoding='ascii') as f:\nfor row in f:\n output.write(row)\n",
2017-05-05T10:07:32
yy