Jump to content

python, codecs, and seek() (help please)

Featured Replies

for the file 'new file' that contains:

line1
line2
line3
line4
line5

 

I get this:

 

Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32) 
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import codecs
>>> test = codecs.open('new file','r','utf')
>>> test.readline()
u'line1\n'
>>> test.readline()
u'line2\n'
>>> test.seek(0)
>>> test.tell()
0L
>>> test.readline()
u'line3\n'
>>> 

 

am i misunderstanding codecs, or is this a bug? it seems to not be reading from where i tell codecs to seek to, even tho tell() indicates that it's seek()d there...

  • Author

short answre: because i've done it another way and there'd be lots of stuff to change if i wanted to do it that way now.

 

long answre, the program it's for is essentially a dictionary-lookup program. atm the dictionary looks kinda like this:

 

start of a: 124
start of b: 3456
start of c: 209834
axx    |
axx    |  decreasing frequency of use
axx    V
bxx
bxx
bxx
cxx
cxx
cxx

 

to look up a word (eg, 'banana'), the program reads the index, seek()s to the beginning of the 'b' words, then reads down till it finds 'banana' (and, the more commonly used a word is, the nearer to the top of it's section it is). also, if you're looking for 'banana' and start to find words that don't start with 'b', the program gives up early. tbh, i'm not sure wether it'd go quicker as a list because it's in memory, or slower because it's unoptomised, and i'm not sure how to optomise a list lookup?

 

i suppose i could load it into memory as a dict{} which is persumably optomised for me, but i'm not sure how big the dictionary will be when finished... i was kinda intending to keep it on the hdd as a favour to the RAM :D

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.