Python Forum

Full Version: How can Read text file and create array?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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'