Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use python to search through Flat Files 1

Status
Not open for further replies.

polka4ever

Technical User
Jan 25, 2006
42
0
0
US
Hello! I am a brand new, novice python user. I'd like to use python to search through some very large flat files that i have. These files will eventually be loaded into a database. The files are fixed width - and i'd like to search each row inbetween a particular range (like between characters 100 - 125, for example). Where do i begin? I've got python installed and up and running!

~p4e
 
I would begin with any of these:



[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
That's a lot of stuff! Thank you for the links. I am browsing through them now. I'm not even sure which function in particular i should be looking for ... any hints?
 
Well you will want to look at os.listdir, open, and slicing for starters.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Good list of links TomThumbKP Thank you! One helped me on a problem I had.


General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
''' a basic script - this doesn't cater for exceptions
take care with using print: if a lot of output is generated
your system will slow down
use output files instead ie:
ofile = open(file,'w') , ofile.write(line) and ofile.close()
'''

import os

def find_it():
dir = os.curdir
for file in os.listdir(dir):
if file[0:3] == 'atp' and file[-4:] == '.dat':
ifile = open(dir+'/'+file,'rb')
for line in ifile:
if line[6:8] == '12':
print line[0:30]
ifile.close()

raw_input('Enter to Exit')

if __name__ == '__main__':
find_it()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top