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
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 758 Nov-04-2025, 11:51 AM
Last Post: buran
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 2,848 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 2,444 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 2,962 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 7,829 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 4,734 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Create Choices from .ods file columns cspower 3 2,725 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 14,449 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 5,691 Nov-09-2023, 10:56 AM
Last Post: mg24
  Create csv file with 4 columns for process mining thomaskissas33 3 2,555 Nov-06-2023, 09:36 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.