[Tutor] newbie question

Karl Pflästerer sigurd@12move.de
Fri Jul 18 11:17:02 2003


On 18 Jul 2003, Wilhelmsen Jan <- Jan.Wilhelmsen@bilia.no wrote:

> I have a text file witch contains some constants that I will use later when
> parsing some other text files.

> File 1:

> 40            444
> 60            380
> 68            950
> 70            950



> What I have to do is to check to values in column 1(v1) against a value in
> another a text string(v2), if match then v1 = v2.

Can you show with a littlem example what you exactly want to achieve.
Are the values in v2 strings which contain v1 as substring? What happens
with column 2 of your example? You don't semm to need it; you mention
only column1 (== v1) and the value in the second text file (== v2).  Is
that value also in column 1?

> I know have to do this by opening the file and do a search and then close
> the file.

> But since this check will occur numerous times I thought maybe it's better
> too read the file in memory once and that's it.

> Can I make sort of a "table" (t1) with objects (column1) that has a
> value(column2). 

Yes.

> Then check this "table" when parsing the other text file?
> Hope someone can give me some tip about this.

Python has a builtin sequence type called dictionary (a hash table).
Perhaps you could use it (you don't say if the order of the entries
matters (if yes a dictionary won't help)).

To build the dictionary you could simple open the file read the lines
and use the first value as key and the second as value of the
dictionary. Like that eg:

>>> table = {}
>>> f = open("file", "r")
>>> for line in f:
...     line = line.split()
...     table[line[0]] = line[1]
... 
>>> table
{'60': '380', '68': '950', '40': '444', '70': '950'}
>>> 

Now you have a dictionary where column 1 of your file acts as key and
column 2 is the value.

Now to the check
You should give a little example first what you exactly like to achieve
otherwise it's not easy to help you.


   Karl
-- 
Please do *not* send copies of replies to me.
I read the list