-2

I have this code that iterates a text file, and each line will contain a companys name. I then want the loop to make a list with the name of that specific line. So let's say I have a file with: Volvo, Audi, BMW

Then the program will do something like the following:

for line in textfile:
         line = []

So now I should have three empty lists: Volvo, Audi and BMW.

I'm sorry if this was confusing, what I'm really trying to ask is, is it possible to initialize a variable with a string name? lets say I have a string car = "volvo". Can I make a new variable, for example a list, with the name of the car object?

Jullix993
  • 105
  • 10
  • You want to create a variable with the name of the company, why? – Anand S Kumar Aug 23 '15 at 15:50
  • @AnandSKumar: No, the OP's just using bad terminology. They want to read the text file line by line, saving the contents to a list. But please don't answer - this question should be closed as a duplicate. – PM 2Ring Aug 23 '15 at 15:52
  • @PM2Ring By the way, you should close vote with the duplicate question as well. – Anand S Kumar Aug 23 '15 at 15:55
  • @AnandSKumar: I was about to CV, but I was just making a quick check first to see if there was a better dupe target in the [SO Python list](http://sopython.com/canon) – PM 2Ring Aug 23 '15 at 15:56
  • @AnandSKumar "Python: read file line by line into array " That's not what I'm trying to do. I want to make a list object with the name of the line in the file. And as I said I have no idea if this is even possible. So if the line in the file is "volvo" I want to make an empty list with the name "volvo". Do you understand what I mean? – Jullix993 Aug 23 '15 at 15:59
  • Like I asked in my first comment - And why do you want that? What benefit do you see from doing that? – Anand S Kumar Aug 23 '15 at 16:07
  • @Jullix993: But your questiomn states that you want a list object named `companies` such that `companies = ['Volvo', 'Audi', 'BMW']`. – PM 2Ring Aug 23 '15 at 16:11
  • Perhaps what you _really_ need is a `dict` object containing the company names as keys, eg `companies = {'Audi': [], 'BMW': [], 'Volvo': []}`, which is easily done using the `dict.fromkeys()` method. Eg, `companies = dict.fromkeys(['Volvo', 'Audi', 'BMW'], [])` – PM 2Ring Aug 23 '15 at 16:14
  • 1
    @PM2Ring Don't do that , that causes all the keys to point to the exact same list. – Anand S Kumar Aug 23 '15 at 16:28
  • Mods, please: The question might be of poor quality, but it's _not_ a duplicate of a question "how to read file into a list". Here, the OP just wants to use locals() instead of a normal explicit dict for their purposes. I can provide a detailed answer if "duplicate" flag is removed. – Veky Aug 23 '15 at 17:00
  • @Veky That would be great. And indeed, it's not a duplicate – Jullix993 Aug 23 '15 at 18:31
  • @YOBA already provided the answer. Thanks for quick reaction. – Veky Aug 23 '15 at 21:23
  • @AnandSKumar: Oops! So it does. :embarrassed: I should know better than to write code 5 minutes before going to bed. :) At least it's only in a comment, not an answer. – PM 2Ring Aug 24 '15 at 07:27

1 Answers1

2

What you need is a dictionary, so you can do:

my_companies = {}

for line in textfile:
    my_companies[line] = []

Now you can access your lists by the company names of a single dictionary

examples:

print my_companies["Volvo"]
print my_companies["Audi"]
print my_companies["BMW"]
YOBA
  • 2,759
  • 1
  • 14
  • 29