Extracting data from a text file using Python

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 uncertainty. All information is formatted as described above, and there is a separator '|' between each descriptor. I want to be able to search for a file for "NAME" and print each descriptor in its own tag, such as this example:

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

In case I am still confused, I want to be able to search for a name and then print out the information following each, separated by a “|” character.

Can anyone help?

EDIT The following is an example of a portion of a text file:

| Trevor Jones | 70 | AST | White | Earth | 3 || 500 | 1500 | Old retired man |

This is the code that I still have:

  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])) 
+4
source share
2 answers

Sort of

 #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 using

 to_search[NAME] 

Depending on the format in which you want to use the answers

 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 are not a more complex solution, may be required.

+2
source

First you need to split the file somehow. I think the 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
source

All Articles