Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting data from txt's
#1
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
Reply
#2
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020