Saturday, September 7, 2013

Reading from two files at once ? Beginner Python 3.x

Reading from two files at once ? Beginner Python 3.x

I'm working on a program that reads from two files. One file contains a
list of words with the number of syllables next to them for example:
happy 2
beautiful 3
music 2
extraordinary 6
The other file contains a mixture of the words in the first file for example:
happy
beautiful music cat
dragon blue pink
I need my program to add up the total number of syllables on each line
using the first file. I made the first file into a dictionary, and that
was all I could do. As a beginner, I'm not sure where else to go on from
here. My tutor is busy and would take really long to reply. My program so
far:
d = {}
with open('file1.txt') as f:
for line in f:
(key, val) = line.split()
d[key] = val
d[val] = key
basically stores the words in the first file as a dictionary, with the
corresponding number of syllables next to it. How do I get my program to
also read the second file and count the number of syllables in each line,
and print out the first line that contains 5 syllables, then the next line
that contains 7 syllables and the next line that contains 5 syllables? I
was thinking maybe storing them in a dictionary, looping over them and
going on from there but I'm not sure how to do that.

No comments:

Post a Comment