[Tutor] how to read two files and substitute
Michiel Overtoom
motoom at xs4all.nl
Tue May 17 16:02:57 CEST 2011
On 2011-05-17 15:42, lina wrote:
> I want to get the $4(column 4) value which has the $1 value. for
> values in file2
Have you tried to write some code yourself already? Please show it.
Anyway, I'd proceed with something like this:
mapping={}
for line in open("file1").readlines():
parts=line.strip().split()
mapping[parts[0]]=parts[3]
origs=open("file2").read().split()
print " ".join([mapping[orig] for orig in origs])
The output is "173 174 174". Since you provide no column names I can't
use meaningful variable names. Are these very big files? In that case
another approach is maybe better.
Greetings,
--
"Good programming is not learned from generalities, but by seeing how
significant programs can be made clean, easy to read, easy to maintain
and modify, human-engineered, efficient, and reliable, by the application
of common sense and good programming practices. Careful study and
imitation of good programs leads to better writing."
- Kernighan and Plauger, motto of 'Software Tools'
More information about the Tutor
mailing list