Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing to a text file?
#1
import csv

with open('animals3.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
           
            print(row[0])
            print(row[1])
            print(row[2])
How do you write the results into a text file shown below
I have looked at different videos but cant figure it out
Thank you


[Image: 2w4h1fk.jpg]
Reply
#2
open a text file:
with open('myfilename.txt', 'w') as fp:
    for row in rows:
        fp.write(row)
But the csv file is already a file, and can be used as any other text file.
Reply
#3
(Oct-12-2018, 12:58 AM)Larz60+ Wrote: open a text file:
with open('myfilename.txt', 'w') as fp:
    for row in rows:
        fp.write(row)
But the csv file is already a file, and can be used as any other text file.

I typed this
import csv
 
with open('animals3.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
with open('myfilename.txt, 'w') as fp:
    for row in rows:
         fp.write(row)
Error I got:
NameError:name 'rows' is not defined

Error:
[error]
[/error]
(Oct-12-2018, 01:25 AM)Bigjay45 Wrote:
(Oct-12-2018, 12:58 AM)Larz60+ Wrote: open a text file:
with open('myfilename.txt', 'w') as fp:
    for row in rows:
        fp.write(row)
But the csv file is already a file, and can be used as any other text file.

I typed this
import csv
 
with open('animals3.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
with open('myfilename.txt, 'w') as fp:
    for row in rows:
         fp.write(row)
Error I got:
Error:
NameError:name 'rows' is not defined
Reply
#4
Check these two links out for pin-pointed info:
https://automatetheboringstuff.com/chapter14/
https://automatetheboringstuff.com/chapter8/
Hope that helps,
Phil
Reply
#5
It should be something like this (untested):
import csv
  
with open('animals3.csv') as csvfile, open('myfilename.txt, 'w') as fp:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
         fp.write(row)
Again this is rather pointless as a csv file is a text file!
you can read as:
with open('animals3.csv') as fp:
    for line in fp:
        print(line.strip()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,061 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing into 2 text files from the same function paul18fr 4 1,628 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,575 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Writing to External File DaveG 9 2,406 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,840 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Writing to file ends incorrectly project_science 4 2,641 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,247 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,325 Nov-16-2020, 07:56 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020