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
  Create Choices from .ods file columns cspower 3 519 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,784 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,309 Nov-09-2023, 10:56 AM
Last Post: mg24
  Create csv file with 4 columns for process mining thomaskissas33 3 695 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  How to read module/class from list of strings? popular_dog 1 424 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 699 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  read file txt on my pc to telegram bot api Tupa 0 1,048 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,043 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,161 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

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