简体   繁体   中英

python - how to write to a csv file while in a loop

I have this piece of code that does a loop:

for i in os.listdir(os.getcwd()):
   if i.endswith(".gz"):
        myfilename=i
        getHum=int(subprocess.check_output('''zcat %s |  awk 'BEGIN {FS=";"};{print $6}' | grep -i 'hummer' | wc -l''' % myfilename, shell=True))
        getBMW=int(subprocess.check_output('''zcat %s |  awk 'BEGIN {FS=";"};{print $6}' | grep -i 'bmw' | wc -l''' % myfilename, shell=True))
        getAudi=int(subprocess.check_output('''zcat %s |  awk 'BEGIN {FS=";"};{print $6}' | grep -i 'audi' | wc -l''' % myfilename, shell=True))

Now, I'm trying to modify it so I write the output to a comma sep csv file. How do I loop through each file and write the extracted contents to a csv file? Thanks.

I think something like this would do what you want:

def get_brand_count(filename, brand):
    return int(subprocess.check_output('''zcat %s |  '''
                                       '''awk 'BEGIN {FS=";"};{print $6}' | '''
                                       '''grep -i %r | wc -l''' %
                                       (filename, brand), shell=True))

brands = 'hummer', 'bmw', 'audi'
with open('results.csv', 'wb') as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerow(brands)  # csv header row (if desired)
    for myfilename in glob.iglob('*.gz'):
        csvwriter.writerow([get_brand_count(myfilename, brand)
                                for brand in brands])

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