Python Forum
Read strings and numbers in columns from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read strings and numbers in columns from a file
#1
Hi all,

I have a .txt file which has two columns. the first column is string and the second column is number. The structure of the .txt file is the following:

abcde  12345
aswd   65892
qwerty 45896
How can I create two lists where list1 is the first column and list2 is the second column?
Any help is much appreciated!
Reply
#2
What have you tried?
Reply
#3
So I tried using numpy but I realized that numpy can't handle strings so I was thinking of using something like this:
data = open('file.txt', 'r').read()
If could only had integers, I could use numpy like this:

data = np.loadtxt('file.txt')
lst1 = data[:, 0]
lst2 = data[:, 1]
But I don't know how I can implement both.

Can anyone please suggest me what I should do here?
Reply
#4
np.loadtxt should be provided with dtypes (however, this is not providing 'two lists' as required):

import numpy as np
arr = np.loadtxt('data.txt', dtype={'names': ('string', 'integer'), 
                                    'formats': ('S6', 'i4')})

# array([(b'abcde', 12345), (b'aswd', 65892), (b'qwerty', 45896)],
#      dtype=[('string', 'S6'), ('integer', '<i4')])
To have two separate lists with plain Python one can just:

texts = []
nums = []

with open('data.txt', 'r') as f:
    for line in f:
        text, num = line.strip().split()
        texts.append(text)
        nums.append(int(num))

print(f'{texts=}')
print(f'{nums=}')

# texts=['abcde', 'aswd', 'qwerty']
# nums=[12345, 65892, 45896]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Thank you so so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,141 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 1,275 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,602 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,809 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,334 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Create Choices from .ods file columns cspower 3 1,773 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 4,825 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,882 Nov-09-2023, 10:56 AM
Last Post: mg24
  Create csv file with 4 columns for process mining thomaskissas33 3 1,817 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  How to read module/class from list of strings? popular_dog 1 1,325 Oct-04-2023, 03:08 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