Tom F
Tom F

Reputation: 453

Searching through data in txt files

I'm teaching myself python and wanted to learn how to search through text files. For example, i've got a long list of full names and addresses, and want to be able to type in a first name and then print the details corresponding to that name. What would be the best way to go about this? Thanks!

The data I have is in a .txt file in columns like this:

Doe, John        London
Doe, Jane        Paris

Upvotes: 1

Views: 1697

Answers (4)

arshajii
arshajii

Reputation: 129477

You could do something as simple as this:

name = raw_input('Type in a first name: ')  # name to search for

with open('x.txt', 'r') as f:  # 'r' means we only intend to read
    for s in f:
        if s.split()[1] == name:  # s.split()[1] will return the first name
            print s
            break  # end the loop once we've found a match
    else:
        print 'Name not found.'  # this will be executed if no match is found
Type in a first name: Jane
Doe, Jane        Paris

Relevant documentation

Upvotes: 0

Sindre Johansen
Sindre Johansen

Reputation: 108

To read a text-file in python, you do something like this:

f = open('yourtextfile.txt')
for line in f:
    //The for-loop will loop thru the whole file line by line
    //Now you can do what you want to the line, in your example
    //You want to extract the first and last name and the city

Upvotes: 0

Thane Brimhall
Thane Brimhall

Reputation: 9555

I would make judicious use of the split command. It depends on how your file is delimited, of course, but your example shows that the characters splitting the data fields are spaces.

For each line in the file, do something like this:

last, first, city = [data.strip(',') for data in line.split(' ') if data]

And then run your comparison based on those attributes.

Obviously, this will break if your data fields have spaces in them, so ensure that's not the case before you take a simple approach like this.

Upvotes: 1

abarnert
abarnert

Reputation: 365587

If you've designed the data format, fixed-width columns aren't a very good one. But if you're stuck with them, they're easy to deal with.

First, you want to parse your data:

addressbook = []
with open('addressbook.txt', 'r') as f:
    for line in f:
        name, city = line[:17], line[17:]
        last, first = name.split(',')
        addressbook.append((first, last, city))

But now, you want to be able to search by first name. You can do that, but it might be slow for a huge addressbook, and the code won't be very direct:

def printDetails(addressbook, firstname):
    for (first, last, city) in addressbook:
        if first == firstname:
            print fist, last, city

What if, instead of just a list of tuples, we used a dictionary, mapping first names to the other field?

addressbook = {}
with open('addressbook.txt', 'r') as f:
    for line in f:
        name, city = line[:17], line[17:]
        last, first = name.split(',')
        addressbook[first]=((last, city))

But that's no good—each new "John" will erase any previous "John". So what we really want is a dictionary, mapping first names to lists of tuples:

addressbook = collections.defaultdict(list)

with open('addressbook.txt', 'r') as f:
    for line in f:
        name, city = line[:17], line[17:]
        last, first = name.split(',')
        addressbook[first].append((last, city))

Now, if I want to see the details for that first name:

def printDetails(addressbook, firstname):
    for (last, city) in addressbook[firstname]:
        print firstname, last, city

Whichever way you go, there are a few obvious places to improve this. For example, you may notice that some of the fields have extra spaces at the start or end. How would you get rid of those? If you call printDetails on "Joe" and there is no "Joe", you get nothing at all; maybe a nice error message would be better. But once you've got the basics working, you can always add more later.

Upvotes: 1

Related Questions