4

So I have a large text file. It contains a bunch of information in the following format:

|NAME|NUMBER(1)|AST|TYPE(0)|TYPE|NUMBER(2)||NUMBER(3)|NUMBER(4)|DESCRIPTION|

Sorry for the vagueness. All the information is formatted like the above and between each descriptor is the separator '|'. I want to be able to search the file for the 'NAME' and the print each descriptor in it's own tag such as this example:

Name
Number(1):
AST:
TYPE(0):
etc....

In case I'm still confusing, I want to be able to search the name and then print out the information that follows each being separated by a '|'.

Can anyone help?

EDIT Here is an example of a part of the text file:

|Trevor Jones|70|AST|White|Earth|3||500|1500|Old Man Living in a retirement home|

This is the code I have so far:

 with open('LARGE.TXT') as fd:
    name='Trevor Jones'
    input=[x.split('|') for x in fd.readlines()]
    to_search={x[0]:x for x in input}
    print('\n'.join(to_search[name]))

2 Answers 2

2

First you need to break the file up somehow. I think that a dictionary is the best option here. Then you can get what you need.

d = {}
# Where `fl` is our file object
for L in fl:
    # Skip the first pipe
    detached = L[1:].split('|')
    # May wish to process here
    d[detached[0]] = detached[1:]
# Can do whatever with this information now
print d.get('string_to_search')
2

Something like

#Opens the file in a 'safe' manner
with open('large_text_file') as fd:
    #This reads in the file and splits it into tokens, 
    #the strip removes the extra pipes  
    input = [x.strip('|').split('|') for x in fd.readlines()]
    #This makes it into a searchable dictionary
    to_search = {x[0]:x for x in input}

and then search with

to_search[NAME]

Depending on the format you want the answers in use

print ' '.join(to_search[NAME])

or

print '\n'.join(to_search[NAME])

A word of warning, this solution assumes that the names are unique, if they aren't a more complex solution may be required.

6
  • Can you expand on how I'd search with 'to_search[NAME]'? Commented Mar 24, 2013 at 1:11
  • The code to_search[NAME] where NAME is a name (as a string) will give you all the data associated with that name.
    – jhoyla
    Commented Mar 24, 2013 at 1:15
  • I'm trying to implement your code and it's coming up with a keyerror saying "KeyError: 'NAME'". Not too sure what I'm doing wrong. Commented Mar 24, 2013 at 1:24
  • You need to replace NAME with a name in the file, I'm using it as a place-holder because you didn't provide any sample data.
    – jhoyla
    Commented Mar 24, 2013 at 1:27
  • Alright I edited the question to help clarify things. I'm getting the error "KeyError: Trevor Jones" Commented Mar 24, 2013 at 1:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.