0

I hope you can help me with a very specific thing that I want to do in my python code. So, I want to create a concrete number of csv files, I mean, I want to create a csv file for each material in a storage. At this moment I have:

nMat = 2 
for i in range(nMat):
      with open(infoVar[i].csv,'w') as f:
              writer = csv.writer(f, delimiter='\t', lineterminator='\n---------------\n')
              writer.writerow(['Id' + '  ' + 'Date & Time'])

This code obviously only creates a csv file named infoVar[i] but I wanted to create 2 csv files named infoVar0 and infoVar1 for example or something that creates several csv files.

AleksMat
  • 854
  • 6
  • 11
digs_ef
  • 11
  • 3

2 Answers2

1

You can't do such a thing with plain string.

You need to format it with 'infoVar{}.csv'.format(i)

Or using Python3.6 f-string f'infoVar{i}.csv'

vishes_shell
  • 22,409
  • 6
  • 71
  • 81
0
nMat = 2 
for i in range(nMat):

      filename = 'infoVar{i}.csv'.format(i)

      with open(filename,'w') as f:
              writer = csv.writer(f, delimiter='\t', lineterminator='\n---------------\n')
              writer.writerow(['Id' + '  ' + 'Date & Time'])

Here's a SO question to read up on about string formatting in Python - please take a look!