rhb1
rhb1

Reputation: 75

Read each line of a text file and get the first split string

I want to read a text file link123.txt from my python code. The text file contains 3 lines.

www.link1.com | linkname1
www.link2.com | linkname2
www.link3.com | linkname3

I need to assign the first portion(url) of the line in "target" variable & second portion(link name) to "name" variable(for further usage).

So far the code:

f = open("link123.txt")
target = [url.strip() for url in f.readlines()]
f.close()

My question is how can I get two value from a line which is split by (" | ") Any guidance would be much appreciated.

Upvotes: 1

Views: 1585

Answers (3)

AChampion
AChampion

Reputation: 30278

Do you need all the names and all the targets in a different variables, e.g.:

In [1]:
with open("link123.txt") as f:
    targets, names = zip(*[url.strip().split(' | ') for url in f])
targets, names

Out[1]:
(('www.link1.com', 'www.link2.com', 'www.link3.com'), 
 ('linkname1', 'linkname2', 'linkname3'))

But you may want to consider holding them in a dict:

In [2]
with open("link123.txt") as f:
    data = [dict(zip(['target', 'name'], url.strip().split(' | '))) for url in f]
data

Out[2]:
[{'name': 'linkname1', 'target': 'www.link1.com'},
 {'name': 'linkname2', 'target': 'www.link2.com'},
 {'name': 'linkname3', 'target': 'www.link3.com'}]

Upvotes: 1

falsetru
falsetru

Reputation: 369334

You can use str.split to split string by delimiter:

>>> 'www.link1.com | linkname1'.split(' | ')
['www.link1.com', 'linkname1']

Then, use iterable-unpacking (or multiple assignment) to save to variables:

>>> target, name = 'www.link1.com | linkname1'.split(' | ')
>>> target
'www.link1.com'
>>> name
'linkname1'

with open("link123.txt") as f:
    for line in f:
        target, name = line.strip().split(' | ')
        # Do something with `target`, `name`

NOTE: Iterating file object will yield lines. You don't need to use file.readlines() which returns a list of all lines unless you need all lines at once.


UPDATE

If you want list of targets, names, you can use zip:

>>> rows = [['link1', 'name1'], ['link2', 'name2'], ['link3', 'name3']]
>>> zip(*rows)
[('link1', 'link2', 'link3'), ('name1', 'name2', 'name3')]
# In Python 3.x, zip will return `zip` object instead of list.

with open("link123.txt") as f:
    targets, names = zip(*[line.strip().split(' | ') for line in f])
    # targets, names = map(list, zip(...))   # if you want lists isntead of tuples

Upvotes: 1

MarianD
MarianD

Reputation: 14191

Instead of

target = [url.strip() for url in f.readlines()]

use

pairs  = [line.strip().split(' | ') for line in f.readlines()]

target = [pair[0] for pair in pairs]
name   = [pair[1] for pair in pairs]

Upvotes: 1

Related Questions