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
  Read csv file through PyCharm kimx0961 3 120 Yesterday, 07:05 PM
Last Post: perfringo
  Why it does not print(file.read()) Rejaul84 1 246 Jul-01-2021, 10:37 PM
Last Post: bowlofred
  Read and write active Excel file euras 4 370 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  [Solved] Trying to read specific lines from a file Laplace12 7 521 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  [Solved] Using readlines to read data file and sum columns Laplace12 4 313 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Read file, reformat and write new file bryanmartin113 1 345 Jun-08-2021, 09:27 PM
Last Post: Larz60+
  Calcolate the average of numbers from a .txt file francescomiles 2 704 Mar-27-2021, 02:43 PM
Last Post: francescomiles
  Python Matplotlib: Create chart for every 4 columns in Excel file JaneTan 2 708 Feb-28-2021, 05:02 AM
Last Post: JaneTan
  Read/Write binary file deanhystad 3 738 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  csv.reader(): Limit the number of columns read in Windows Pedroski55 9 1,123 Jan-23-2021, 01:03 AM
Last Post: pjfarley3

Forum Jump:

User Panel Messages

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