0

This is and example of what my csv file looks like with 6 columns:

0.0028,0.008,0.0014,0.008,0.0014,0.008,

I want to create 6 variables to use later in my program using these numbers as the values; however, the number of columns WILL vary depending on exactly which csv file I open.

If I were to do this manually and the number of columns was always 6, I would just create the variables like this:

thickness_0 = (row[0])
thickness_1 = (row[1])
thickness_2 = (row[2])
thickness_3 = (row[3])
thickness_4 = (row[4])
thickness_5 = (row[5])

Is there a way to create these variables with a for loop so that it is not necessary to know the number of columns? Meaning it will create the same number of variables as there are columns?

CPMAN
  • 69
  • 6
  • You don't need 6 variables. You have a perfectly fine list. – user2357112 Feb 05 '19 at 21:52
  • csv reader will "create as many variables as it has to" for you – sam46 Feb 05 '19 at 21:53
  • to sam46 - ok, but how do I increment the numbers from 0 to 5 when there are 6 rows, and 0 to 17 when I have 18 rows? – CPMAN Feb 05 '19 at 22:45
  • to user2357112 - Yes, it is a fine list. :-) But, I don't want to manually create it and the number of rows varies depending on which csv file is chosen. How do I automatically create variables with the number at the end incrementing as the row number increments? Use a for loop? Probably, but I don't know how to increment the number for the variable name and the row number within the loop. – CPMAN Feb 05 '19 at 23:26
  • 1
    @CPMAN: You don't need to manually create anything. Learn to use lists. Learn to loop over a list. Numbered variables will not actually help you solve your problems. – user2357112 Feb 06 '19 at 01:11

3 Answers3

0

There are ways to do what you want, but this is considered very bad practice, you better never mix your source code with data.

If your code depends on dynamic data from "outer world", use dictionary (or list in your case) to access your data programatically.

Slam
  • 8,112
  • 1
  • 36
  • 44
  • When the program needs to know the thickness of the dielectrics in a printed circuit board stackup (PCB stackup), the information is available in a csv file. On startup of the program, the PCB stackup file will be identified and use whatever the number of dielectrics are in that stackup for the analysis. Depending on the number of dielectrics in the csv file, the number of variables needed to be created will vary. What I want to do here is some kind of for loop and automatically increment the number of the variable name to match the row number of the value extracted from the csv file. – CPMAN Feb 05 '19 at 22:47
0

You can use a dictionary

mydict = {}

    with open('StackupThick.csv', 'r') as infile:
        reader = csv.reader(infile, delimiter=',')
        for idx, row in enumerate(reader):
            key = "thickness_" + str(idx)
            mydict[key] = row

Call your values like this:

print(mydict['thickness_3'])
Mike C.
  • 1,761
  • 2
  • 22
  • 46
  • While this is a thing that can be done, it would be simpler to use a list. – user2357112 Feb 08 '19 at 07:34
  • I agree but since op wanted to use variables, I thought the key/value features of a dictionary would offer him the same functionality with less headache. – Mike C. Feb 08 '19 at 13:57
-1

From your question, I understand that your csv files have only one line with the comma separated values. If so, and if you are not fine with dictionares (as in @Mike C. answers) you can use globals() to add variables to the global namespace, which is a dict.

import csv

with open("yourfile.csv", "r", newline='') as yourfile:
    rd = csv.reader(yourfile, delimiter=',')
    row = next(rd)
    for i, j in enumerate(row):
        globals()['thickness_' + str(i)] = float(j)

Now you have whatever number of new variables called thickness_i where i is a number starting from 0.

Please be sure that all the values are in the first line of the csv file, as this code will ignore any lines beyond the first.

Valentino
  • 7,291
  • 6
  • 18
  • 34
  • This is utterly pointless and offers no advantages over using the list directly. Also, modifying `locals()` is [not actually a supported way to assign local variables](https://ideone.com/f3JFXu). – user2357112 Feb 08 '19 at 07:32
  • My bad about `locals()`, I admit I did not know. I'll remove that from my answer. I know that the list itself can be easily used directly, but the OP asked explicitly for this. – Valentino Feb 08 '19 at 11:54
  • Valentino - I will try what you have suggested and post if it works or blows-up as others say it ultimately will. I will also try using the list directly... Thanks! – CPMAN Feb 08 '19 at 12:09
  • Valentino - I have tried your globals approach and for me it works great! I can read any of the stackup files (with any number of layers) and get the values I need (based on the global name) for a number of different functions in the program. Thanks! – CPMAN Feb 08 '19 at 12:33