python - Combine Name and number save fix amount -


i troubled little code. reading csv file adding column , chanching delimiter.

that works perfect.

but, column want give in name, stuck on problem.

when add column, want fill in name: l_panoramic_000000 , want keep on giving numbers, within 6 digits. this:

some examples (of course numbering trough, example skipt some). l_panoramic_000000 l_panoramic_000001 l_panoramic_000010 l_panoramic_009523

--

now like: l_panoramic_0000001 l_panoramic_0000002 l_panoramic_0000004 l_panoramic_00000010 l_panoramic_000000125 l_panoramic_0000002568 etc

how can fix keep in 6 digits?

for incsv in folder_in.iterdir():     outcsv = folder_out.joinpath('0new'+incsv.name)      open(str(incsv), 'rb') input, open(str(outcsv), 'wb') output:         reader = csv.reader(input, delimiter = ',')         writer = csv.writer(output, delimiter = ';')          = []         row = next(reader)         row.insert(0, 'l_panoramic')         all.append(row)         k, row in enumerate(reader):             all.append(['l_panoramic_000000'+str(k+0)] + row)         writer.writerows(all) 

with code:

  k, row in enumerate(reader):         all.append(['l_panoramic_'+str.format(k+'06')] + row)         writer.writerows(all) 

it gives me:

 file "c:/code/convert/convertimagetrigger.py", line 20, in <module>     all.append(['l_panoramic_'+str.format(k+'06')] + row) typeerror: unsupported operand type(s) +: 'int' , 'str' 

you can use builtin format() function, e.g.:

'l_panoramic_' + format(k, '06') 

will pad k 0s width of 6.
alternatively, can use str.format() similar format string:

'l_panoramic_{:06}'.format(k) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -