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
#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


Messages In This Thread
Want to Save Print output in csv file - by Rasedul - Jan-11-2022, 02:50 PM
RE: Want to Save Print output in csv file - by snippsat - Jan-11-2022, 07:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot get cmd to print Python file Schauster 11 670 May-16-2024, 04:40 PM
Last Post: xMaxrayx
  Open/save file on Android frohr 0 433 Jan-24-2024, 06:28 PM
Last Post: frohr
  how to save to multiple locations during save cubangt 1 646 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,428 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,239 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,421 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,247 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Save multiple Parts of Bytearray to File ? lastyle 1 1,023 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,483 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Saving the print result in a text file Calli 8 2,036 Sep-25-2022, 06:38 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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