0

I'm kind of new to python here's my code so far. The goal I want to accomplish is to refer a variable name using another variable.

tax_files=['2008','2009','2011','2012','2013','2014','2015']
file_dir='/'
for tax_file in tax_files:
    filename=file_dir + tax_file+'.csv'
    with open(filename,'r') as f:
        for line in f:
            splitted_line = line.split()
            dict_(tax_file)[splitLine[1]] = splitLine[0]

in the last line here, dict(tax_file) is suppose to be different at different iteration eg. first iteration should be dict_2008, then dict_2009 and so forth.

just for easier understanding, the file i read in is like this

usgaap costofgoodsold
usgaap returnonasset

Can I accomplish this in python?

Sam He
  • 79
  • 1
  • 8
  • Can you just use a dictionary to store the variable names and values? – lwassink Jul 06 '16 at 22:22
  • use another dict to contain the other dicts, for example all_tax[tax_file], with the filename (or just year) as key, and as value the dict for that year – λuser Jul 06 '16 at 22:22
  • Your question is unclear! Edit to show what you want to achieve i.e. show the output you are expecting in the code, by hardcoding! – Devi Prasad Khatua Jul 06 '16 at 22:28
  • @λuser@Iwassink Thank you! that should work, a dictionary of a dictionary, but is this a good structure to code in sense of memory management? – Sam He Jul 07 '16 at 12:52

2 Answers2

1
res = {}
for tax_file in tax_files:
    filename=file_dir + tax_file+'.csv'
    with open(filename,'r') as f:
        for line in f:
            splitted_line = line.split()
            res[ tax_file + '_' +splitLine[1]] = splitLine[0]

update

res = {}
for tax_file in tax_files:
    temp = {}
    filename=file_dir + tax_file+'.csv'
    with open(filename,'r') as f:
        for line in f:
            splitted_line = line.split()
            temp[line[0]] = temp.get(line[0],[]) +[line[1]]
   res[ tax_file ] = temp
galaxyan
  • 5,944
  • 2
  • 19
  • 43
  • i think he/she wants to group data by file, but you put everything in a single dict – λuser Jul 06 '16 at 22:24
  • @λuser yup, thanks for clearing it. that's the method I wanted to take to solve this problem, but i will settle at the dictionary method. Just curious is there a way of doing this tho. in the other post [variable variable](http://stackoverflow.com/questions/1373164/how-do-i-do-variable-variables-in-python) I only see it construct the name attribute of an object with getattr or using globals() for accessing, but is it possible to play around of the idea of variable of variable in python? – Sam He Jul 07 '16 at 13:57
0

Why not just make the dict variable a dictionary where the key is the year, and the value is just another dictionary. So that you can do

dict[tax_file][splitLine[1]] = splitLine[0]

This is sort of the standard way to solve the problem you're describing. And in this case you should probably also use a different name for the variable, since 'dict' is a class in python

Shalan
  • 176
  • 5