Python Forum
Want to Save Print output in csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Want to Save Print output in csv file
#1
Sad 
I want to save this print output to CSV file but this code is not working, how can I save this output to CSV?

import csv

for x in range(2000, 3000):
    print('hi', x, sep='')

f = open('op2.csv', 'w')
writer = csv.writer(f)
writer.writerow(print())
f.close()
Reply
#2
print() is a function. When you call the print() function it returns the value None. writerow(print()) will write None in the CSV file. If you want to print() a string and write the same string to a file you need to create the string, assign it to a variable, print the variable and write the variable to the file.
Reply
#3
(Jan-11-2022, 03:09 PM)deanhystad Wrote: print() is a function. When you call the print() function it returns the value None. writerow(print()) will write None in the CSV file. If you want to print() a string and write the same string to a file you need to create the string, assign it to a variable, print the variable and write the variable to the file.

Can you write the code please, I am new in python and learning.
Reply
#4
for a simple csv you don't need csv module, csv is also just a text file

with open('op2.csv', 'w') as f:
    for x in range(2000, 3000):
        row = f'hi{x}\n'
        f.write(row) # write line to file
BashBedlam and Rasedul like this post
Reply
#5
(Jan-11-2022, 04:29 PM)Axel_Erfurt Wrote: for a simple csv you don't need csv module, csv is also just a text file

with open('op2.csv', 'w') as f:
    for x in range(2000, 3000):
        row = f'hi{x}\n'
        f.write(row) # write line to file

It works, Thank you so much!
Reply
#6
(Jan-11-2022, 04:03 PM)Rasedul Wrote: Can you write the code please, I am new in python and learning.
You should give a try when a get suggestion doesn't matter it it's a bad way or not work,just to show some effort Wink
It's not need here to use csv module as pointed out,to show examples how it can be using csv module.

The same output as print().
import csv

with open('op2.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    var = 'hi'
    for x in range(2, 5):
        writer.writerow([f'{var}{x}'])
Output:
hi2 hi3 hi4
A comma between variable and loop result.
import csv

with open('op2.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    header = ['Word', 'Result']   
    var = 'hi'
    writer.writerow(header)
    for x in range(2, 5):
        writer.writerow([var, x])
Output:
Word,Result hi,2 hi,3 hi,4
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 307 Jan-24-2024, 06:28 PM
Last Post: frohr
  how to save to multiple locations during save cubangt 1 537 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,187 Jul-07-2023, 05:44 PM
Last Post: flash77
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,086 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 2,980 Feb-20-2023, 07:19 PM
Last Post: avd88
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,073 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Save multiple Parts of Bytearray to File ? lastyle 1 934 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  How to output one value per request of the CSV and print it in another func? Student44 3 1,316 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Saving the print result in a text file Calli 8 1,769 Sep-25-2022, 06:38 PM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,278 Sep-08-2022, 01:44 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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