Need to create multiple variables based on number of columns in csv file python

python csv write to specific column
pandas dataframe
pandas read_csv
pandas create dataframe
get number of rows and columns in csv python
pandas select columns
create csv file with column names python
python write csv example

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


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?

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.

How to add a column to a CSV file in Python, How do I add a column to a CSV file in Python? The reader function is formed to take each line of the file and make the list of all columns. Then, you choose the column you want a variable data for. The CSV file is opened as the text file with Python’s built-in open () function, which returns the file object. This is then passed to the reader, which does the heavy lifting.

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.

14.1. csv — CSV File Reading and Writing, The so-called CSV (Comma Separated Values) format is the most common CSV format was used for many years prior to attempts to describe the format in These differences can make it annoying to process CSV files from multiple sources. [1] An optional dialect parameter can be given which is used to define a set of  To create multiple variables you can use something like below, you use a for loop and store a pair of key-value, where key is the different variable names d={} #empty dictionary for x in range(1,10): #for looping d["string{0}".format(x)]="Variable1"

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:


Creating Pandas DataFrames & Selecting Data, This lesson of the Python Tutorial for Data Analysis covers creating a pandas DataFrame and A table with multiple columns is a DataFrame. of different data structures and files, including lists and dictionaries, csv files, excel files, data = datasets[0] # assign SQL query results to the data variable Location Based Ads. Add a column to an existing csv file, based on values from other columns. Let’s append a column in input.csv file by merging the value of first and second columns i.e. # Add column to csv by merging contents from first & second column of csv add_column_in_csv('input.csv', 'output_3.csv', lambda row, line_num: row.append(row[0] + '__' + row[1]))

Cookbook, written for Python 3. Minor tweaks might be necessary for earlier python versions. Shift groups of the values in a column based on the index. In [135]: df = pd. 2. Read and Print Specific Columns from The CSV Using csv.reader Method. In the following example, it will print the column COUNTRY_NAME, by specifying the column number as 1 (lines[1]). The CSV file is containing three columns, and the column number starts with 0 when we read CSV using csv.reader method.

Create issues using the CSV importer | Jira Core Cloud, You can also create CSV files to perform bulk issue creation and updates. You can base the structure of your CSV file off the default Microsoft Excel CSV format. Multiple values that need to be aggregated into single issue fields The number of column names specified must match the maximum number of values to be  To pull data from a CSV file, you must use the reader function to generate a reader object. The reader function is designed to take each line of the file and make a list of all columns. Then, you just choose the column you want the variable data for. It sounds a lot more complicated than it is.

Split csv into multiple files based on row python, Python Script to split CSV files into smaller files based on number of lines. The output Secondly, how would I decide what files to create and where to import what and where? Suppose you have column or variable names in second row. Encoding categorical variables is an important step in the data science process. Because there are multiple approaches to encoding variables, it is important to understand the various options and how to implement them on your own data sets. The python data science ecosystem has many helpful approaches to handling these problems.

  • You don't need 6 variables. You have a perfectly fine list.
  • csv reader will "create as many variables as it has to" for you
  • 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?
  • 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: 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.
  • 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.
  • 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 - 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!
  • 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!
  • 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.
  • While this is a thing that can be done, it would be simpler to use a list.
  • 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.