简体   繁体   中英

How to Write Header only once in Python

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

Does something like this help?


import csv

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']

] 

with open('Test.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerows(toWriteHeader)
    for row in range(10):
        writer.writerows(toWrite)

You'll need to adjust the toWrite values back to variables and the for row part.. but this places a "header" and then rows of data in a csv.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM