SV: [Tutor] newbie question

Wilhelmsen Jan Jan.Wilhelmsen@bilia.no
Mon Jul 21 10:12:50 2003


OK I try to explain a little more detailed.

First of all I have 2 text files witch we want to user for reference =
and
translations.

File 1 looks like this:

40            444
60            380
68            950
70            950
kjeu          847
mnsy          342
kjeu I        984
M=E9g           273

etc...



File 2 is just the same structure as file 1,But of course other values.

Then I have a third file called dataf1.txt

One part of each record in this dataf1.txt looks like this:

00120024311011655      0304      1211      M=E9g       5529260=20

In this case in need to replace M=E9g with 273 and create a new =
datafile and
write the new string in this one.
This is just one of the modifications I have to do but the others are =
pretty
much the same as this.
So if I learn how to do one, the rest will be easier.


I tried too create a dictionary like this:

def t1():
    f =3D open("filename","r")
    for line in f:
        line =3D line.split()
        table[line[0]] =3D line[1]
        print table


But here I get only one dictionary item for both:

Kjeu

And=20

Kjeu I



Does this mean that I can't use the line.split?

When I print out the M=E9g post I get M\xe9g , how can I deal with =
this? This
might not a problem if only the output to the new file is ok.



Regards

Jan Wilhelmsen
IT-Technician
Bilia Personbil as


-----Opprinnelig melding-----
Fra: sigurd@12move.de [mailto:sigurd@12move.de]=20
Sendt: 18. juli 2003 17:12
Til: tutor@python.org
Emne: Re: [Tutor] newbie question

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 =3D 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 (=3D=3D v1) and the value in the second text file (=3D=3D =
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).=20

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 =3D {}
>>> f =3D open("file", "r")
>>> for line in f:
...     line =3D line.split()
...     table[line[0]] =3D line[1]
...=20
>>> table
{'60': '380', '68': '950', '40': '444', '70': '950'}
>>>=20

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
--=20
Please do *not* send copies of replies to me.
I read the list


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


------------------------------------------------------------------------=
----
---
This verifies that this e-mail has been scanned for virus and deemed
virus-free=20
according to F-secure Content Scanner 5.0
Fri, 18 Jul 2003 17:11:59 +0200 GMT
------------------------------------------------------------------------=
----
---