Python Forum
How can Read text file and create array? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How can Read text file and create array? (/thread-10476.html)



How can Read text file and create array? - puneet102 - May-22-2018

I have text file (Media_id) with content and I want to create an array from the file. The content of file is like below:

1234L4
2345L4
3456L5
6384L6

and print values as
a(0)=1234L4
a(1)=2345L4
a(2)=3456L5
a(3)=6384L6


RE: How can Read text file and create array? - snippsat - May-22-2018

Read the file and make a list should to it.
with open('media.txt') as f:
    lst = [i.strip() for i in f]
So now have list and a(0) would in this case be lst[0].
>>> lst
['1234L4', '2345L4', '3456L5', '6384L6']
>>> lst[0]
'1234L4'
>>> lst[2]
'3456L5'