seek in a file
Emile van Sebille
emile at fenx.com
Wed Mar 27 09:18:37 EST 2002
"Andreas Penzel" <mail at andreas-penzel.de> wrote in message
news:a7s4q4$no0i9$1 at ID-69142.news.dfncis.de...
> "Steven Majewski" wrote:
>
> > Are the files too big to fit in memory ?
>
> the file is about 1 MB, i have to test out working
>
> > What do you want to do with the lines ?
> > ( I hope you weren't planning on trying to modify a line in a file:
> > because of the variable length on lines, if you don't write
> > exactly the same number of chars back, you will either overwite
> > the next line, or leave an extra partial line after your edited
> > line. Most text editors read the whole file into memory and then
> > write out a new file after it's modified in memory. )
>
> In the file are barcodes of products, one per line.
> The program has to look if the scanned code from the barcode-scanner
is
> identically with one in the file included. This is important because
the
> barcode-scanner sometimes works not correct. All i need is an
search-routine
> that compares the scanned code with the codes in the file. If there is
an
> code, the programs waits for the next code.
>
Then you probably want to read the whole file in and restructure for
optimal access. The duplicate values for multiple keys is no longer an
issue. Build a look up dictionary and use it:
#--untested--
productlist = open('\path\and\filename').readlines()
upcdict = {}
for rec in productlist:
product, upccode = rec.split(delimiter)
upcdict[upccode] = product # or None if you're just validating
try: upcdict[scannedcode]
except: invalid code handling
HTH,
--
Emile van Sebille
emile at fenx.com
---------
More information about the Python-list
mailing list