Python Forum
How can Read text file and create array?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can Read text file and create array?
#1
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
Reply
#2
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Choices from .ods file columns cspower 3 518 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,782 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  Use PM4PY and create working file thomaskissas33 0 572 Nov-14-2023, 06:53 AM
Last Post: thomaskissas33
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,304 Nov-09-2023, 10:56 AM
Last Post: mg24
  Create csv file with 4 columns for process mining thomaskissas33 3 691 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  read file txt on my pc to telegram bot api Tupa 0 1,047 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,038 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,160 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,002 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,140 Mar-27-2023, 08:59 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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