Python Forum

Full Version: getting data from txt's
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My question getting data from txt files. For example, in a folder i have 12 files lile sample01.txt, sample02.txt, sample03.txt, ..... sample12.txt. In each file i have numeric values like 1,3,7 and 8,9,4 in other (different values but equal number in each file).
here the question how can i import them in python and organise in tuple or list objects as data01, data02, data03 etc.
when trying make it i have 2 problems. first after function for importing one file is finished, data is not reachable. Second, how can i manipulate name of lists automatically (like list01,list02.. or data01,data02)
Thank you for answers
(Apr-11-2020, 10:32 AM)baris013 Wrote: [ -> ]when trying make it i have 2 problems. first after function for importing one file is finished, data is not reachable.

show your code, in python tags\

(Apr-11-2020, 10:32 AM)baris013 Wrote: [ -> ]Second, how can i manipulate name of lists automatically (like list01,list02.. or data01,data02)

You should not create variable names dynamically, use proper data structure.
(Apr-11-2020, 10:32 AM)baris013 Wrote: [ -> ]first after function for importing one file is finished, data is not reachable
Yes you are right. Variables in functions are local and are gone when the function finishes. This is always the case so you have to choose:
  1. Do not use a function to read the data, or
  2. Have the function return the data to the main program, or
  3. Do all the processing in the same function.

(Apr-11-2020, 10:32 AM)baris013 Wrote: [ -> ]Second, how can i manipulate name of lists automatically
Like Buran said: use proper data structure. In this case you had best create a list of lists. A list of as many elements as there are files. And each of the elements should be a list of the items found in the corresponding file.
You may also choose a dictionary of lists or a list of tuples or whatever. I chose a list of lists because the list is the most common structure in Python. If a list does not fulfill your needs, you should think of other structures like dictionaries, tuples, sets.