1

How I read CSV's files dynamically in Python, when change the suffix name files?

Example:

import pandas as pd
uf = ['AC', 'AL', 'AP', 'AM', 'BA', 'CE', 'DF', 'ES', 'GO', 'MA', 'MT', 'MS', 'MG', 'PA', 'PB', 'PR', 'PE', 'PI', 'RJ', 'RN', 'RS', 'RO', 'RR', 'SC', 'SP1', 'SP2', 'SE', 'TO']
for n in uf:
   {n} = pd.read_csv('Basico_{n}.csv', encoding='latin1', sep=';', header=0)

The {} is not recognize into "for-loop".

I want to read the different file suffix names within in list items and create different DataFrames by same rules.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • You can't do that. Nor would you want to. Add them to a dictionary instead of "variable variable names" – roganjosh Jan 18 '20 at 21:05
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – roganjosh Jan 18 '20 at 21:06

2 Answers2

2

You have two main issues:

  1. {n} = is invalid syntax. You can't assign to a variable name without messing with locals or globals. Doing so is almost always a bad idea anyway because it's much more difficult to programmatically access names that are, in a way, hard-coded. If the list of names is dynamic, then you need to start accessing globals() to get at them and this leads to bugs.
  2. 'Basico_{n}.csv' misses the f out of fstrings. n will not be added to the string if you don't specify that it's an f-string by prepending f.

Instead:

import pandas as pd
uf = ['AC', 'AL', 'AP', 'AM', 'BA', 'CE', 'DF', 'ES', 'GO', 'MA', 'MT', 'MS', 'MG', 'PA', 'PB', 'PR', 'PE', 'PI', 'RJ', 'RN', 'RS', 'RO', 'RR', 'SC', 'SP1', 'SP2', 'SE', 'TO']
dfs = {} # Create a dict to store the names
for n in uf:
   dfs[n] = pd.read_csv(f'Basico_{n}.csv', encoding='latin1', sep=';', header=0)
roganjosh
  • 12,594
  • 4
  • 29
  • 46
-2
'Basico_{n}.csv'

Will only work for python >= 3.6

Try

{n} = pd.read_csv('Basico_{}.csv'.format(n), encoding='latin1', sep=';', header=0)

Radonic
  • 47
  • 2
  • 6