python - Write number of rows to file name -


i dont have idea how update code below level can write amount of changed row - in csv - file name.

i have did stuff count , row, making sense.

can give me tips

import csv import glob import os  rows = []  in_csv in glob.glob('c:/code/convert/image/in/*.csv'):     print in_csv      open(in_csv, 'rb') f_input:         reader = csv.reader(f_input)          try:             = []             row = next(reader)             row.insert(0, 'l_panoramic')             all.append(row)              k, row in enumerate(reader):                 all.append(['l_panoramic_{:06}'.format(k)] + row)              name, ext = os.path.splitext(in_csv)              open("{}_{}{}".format(name, len(rows), ext), 'wb') f_output:                 writer = csv.writer(f_output, delimiter = ';')                 writer.writerows(all)                 print  "  {} lines found".format(len(rows))         except stopiteration e:             print "  no lines found" 

this done using glob library create list of csv files. use splitext() take existing filename , split filename , extension, number of rows can added using format() statement follows:

import csv import glob import os  rows = []  in_csv in glob.glob('a*.csv'):     print in_csv      open(in_csv, 'rb') f_input:         reader = csv.reader(f_input)          try:             row = next(reader)             row.insert(0, 'l_panoramic')             rows.append(row)              k, row in enumerate(reader):                 rows.append(['l_panoramic_{:06}'.format(k)] + row)              name, ext = os.path.splitext(in_csv)              open("{}_{}{}".format(name, len(rows), ext), 'wb') f_output:                         writer = csv.writer(f_output, delimiter = ';')                 writer.writerows(rows)                 print  "  {} lines found".format(len(rows))         except stopiteration e:             print "  no lines found" 

you creating list of rows written, once list complete, know how many rows there are. can open output file number of rows added name, , write rows it.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -