Pippi
Pippi

Reputation: 2571

How to split entries of a file into a list of strings in Python?

I have a file such that each line consists of two strings separated by variable space, like below:

"Doe, Mary" "W 135"

How can this be parsed into pairs of strings, ["Doe, Mary", "W 135"]?

Upvotes: 1

Views: 132

Answers (1)

abarnert
abarnert

Reputation: 365707

with open('file.txt') as f:
    pairs = csv.reader(f, delimiter=' ', skipinitialspace=True)

Now you can make a list of pairs, iterate over it in a for loop, whatever.

Upvotes: 2

Related Questions