PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 13, 2022

[FIXED] How to Write Header only once in Python

 May 13, 2022     append, python, python-3.x     No comments   

Issue

I am very new to programming how can I write the header only once but the other values repeatedly i am not sure what terms to use to describe this so i will show you what i mean:

toWriteHeader = [
    ["Timestamp:", "Overall result:","Blank", "Soll-orderno:", "Desired-HW-Version:", "Desired-SF-Version:","Desired-productcode:", "Desired-device-type:", "Scancode:", "Wbm-orderno:", "Wbm-HW-Version:", "Wbm-SF-Version:",
      "Wbm-mac-address:","combined-product-code:", "wbm-device-type:"],
      
    
]   
toWrite = [
    [now ,"Blank",d_ordernum,d_hw_version,d_sf_version,pc_praefix,d_dev_typ,scancode_string,ord_nmr,ord_nmr,v,b]

] 

file = open('Test.csv', 'w') 

with file:
    writer = csv.writer(file)

    for row in toWriteHeader:
        writer.writerow(row)

file = open('Test.csv', 'a') 

with file:
    writer = csv.writer(file)

    for row in toWrite:
        writer.writerow(row)

so basically "writeToHeader" only needs to be written once but the other values need to be repeatedly appended but that does not happen I it is only writting "toWrite" once

can anyone suggest andy solutions or show me an example please. PS: go easy on me i am a beginner

Example output to the file:

Timestamp:, Overall result:,Soll-orderno:,Desired-HW-Version:,Desired-SF-Version:,Desired-productcode:,Desired-device-type:,Scancode:,Wbm-orderno:,Wbm-HW-Version:,Wbm-SF-Version:,Wbm-mac-address:,combined-product-code:,wbm-device-type:

2021-04-26 13:32:11,Blank,58184,1.00,1.0.0,7A2F7,TREE M-5TX PN IP67,58183#99AF0M000F9EF41A80,58184,58184,1.00,1.0.0

2021-04-26 13:32:32,Blank,58184,1.00,1.0.0,7A2F7,TREE M-5TX PN IP67,58183#99AF0M000F9EF41A80,58184,58184,1.00,1.0.0

2021-04-26 13:32:38,Blank,58184,1.00,1.0.0,7A2F7,TREE M-5TX PN IP67,58183#99AF0M000F9EF41A80,58184,58184,1.00,1.0.0

2021-04-26 13:33:48,Blank,58184,1.00,1.0.0,7A2F7,TREE M-5TX PN IP67,58183#99AF0M000F9EF41A80,58184,58184,1.00,1.0.0

2021-04-26 13:33:55,Blank,58184,1.00,1.0.0,7A2F7,TREE M-5TX PN IP67,58183#99AF0M000F9EF41A80,58184,58184,1.00,1.0.0

My Current Output to the file:

Timestamp:, Overall result:,Soll-orderno:,Desired-HW-Version:,Desired-SF-Version:,Desired-productcode:,Desired-device-type:,Scancode:,Wbm-orderno:,Wbm-HW-Version:,Wbm-SF-Version:,Wbm-mac-address:,combined-product-code:,wbm-device-type:

2021-04-26 13:32:11,Blank,58184,1.00,1.0.0,7A2F7,TREE M-5TX PN IP67,58183#99AF0M000F9EF41A80,58184,58184,1.00,1.0.0

Solution

I am assuming that you want to run this program several times and the values in the "toWrite" list change?

If so, your problem is that you always execute the part:

file = open('Test.csv', 'w') 

with file:
    writer = csv.writer(file)

    for row in toWriteHeader:
        writer.writerow(row)

So you open the file in the mode 'w' which overwrites the existing content. Just check if the file exists before you write the header. Also it is better to open files as part of the context manager (with) so that it is automatically closed afterwards:

import os.path

fname = 'HERE SHOULD BE THE FULL PATH OF THE FILE'

if (not os.path.isfile(fname)):
    with open(fname, 'w') as file:
        writer = csv.writer(file)

        for row in toWriteHeader:
            writer.writerow(row)

You have to define the full path to the file in fname before or change to the directory of the file first.

This solution assumes that the file does not exist the first time you run your code to write the header. If it may already exist but may be empty, you can check if it is empty instead: How to check if .xls and .csv files are empty



Answered By - Oskar Hofmann
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing