Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
csv file output
#1
I have some problems:

I created a csv file

    with open('file.csv','a') as f:
        thewriter=csv.writer(f)

        thewriter.writerow({' name',' adult','child','reference'})
        thewriter.writerow([name,ad,ch,ref])
when I run and enter some date for name, adult, child and reference I get the data under different columns. for instance the name column has number rather than the number.

any idea why is that? see the attachment for output in csv file.

Attached Files

Thumbnail(s)
   
Reply
#2
obviously that is what the values for ad, ch and ref names are... We cannot tell what is going on without seeing that part of your code

also note that {' name',' adult','child','reference'} is a set, i.e. unordered container. Use a list or a tuple e.g. ['name','adult','child','reference']

in addition - do you really want to repeat the header above each data row?
Also note that when using csv module on Windows, you need to supply newline='' argument to open(). Not doing so result in extra blank line visible after each header/data row
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jan-30-2020, 10:36 AM)buran Wrote: obviously that is what the values for ad, ch and ref names are... We cannot tell what is going on without seeing that part of your code

also note that {' name',' adult','child','reference'} is a set, i.e. unordered container. Use a list or a tuple e.g. ['name','adult','child','reference']

in addition - do you really want to repeat the header above each data row?
Also note that when using csv module on Windows, you need to supply newline='' argument to open(). Not doing so result in extra blank line visible after each header/data row

Thanks. That is useful. Can let me know how to not repeat header above each data row?
Reply
#4
create the header only once. don't write it with every data row. In addition, you may look at (and use csv.DictReader, which has specual method writeheader(). You may also use writerows() to write multiple rows with one line statement
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Somehow I am getting errors for creating header only once. Huh
The error is [Errno 13] Permission denied
Reply
#6
show your code, however the error suggest you try to open same file for a second time, while it is still open
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
import pickle
import csv

with open('file.csv','a',newline="") as f:
    thewriter=csv.writer(f)

    thewriter.writerow([' name',' adult','child','reference'])
        

ref=-1
av_seats=152

while True:    
    ad=int(input("Enter a  adult "))

    ch=int(input("Enter a  children "))
    name=input("Plese enter your name")

    totoal=ad+ch

    av_seats=av_seats-totoal
    ref=ref+1

    thewriter.writerow([name,ad,ch,ref])



    print("left are ",av_seats)
   
    op=input("press 'n' to terminate or any key to stop ").lower()
    if op=='n':
        break
Reply
#8
Lines 10-32 need to be indented one kevel, so that they are inside the with contex manager
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Output CSV file with filepath and file contents glittergirl 1 1,706 Aug-03-2020, 01:50 AM
Last Post: glittergirl
  File name as part of output file name Jeeping_Coder 1 2,076 Jan-10-2020, 03:43 PM
Last Post: Clunk_Head
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 2,029 Oct-21-2019, 01:01 PM
Last Post: enyrb
  python file output to log file Rsh 4 3,641 Aug-13-2019, 09:00 PM
Last Post: DeaD_EyE
  Output SQL to csv or xls file? JP_ROMANO 4 2,534 Aug-02-2019, 01:58 AM
Last Post: JP_ROMANO

Forum Jump:

User Panel Messages

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