jgree157
jgree157

Reputation: 33

Python parsing to lower case and removing punctuation not functioning properly

import string
remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))

with open('data10.txt', 'r') as f:
for line in f:
    for word in line.split():
        w = f.read().translate(remove)
        print(word.lower())

I have this code here and for some reason, the translate(remove) is leaving a good amount of punctuation in the parsed file.

Upvotes: 1

Views: 732

Answers (1)

mhawke
mhawke

Reputation: 87084

Why are you reading the whole file within the for loop?

Try this:

import string
remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))

with open('data10.txt', 'r') as f:
    for line in f:
        for word in line.split():
            word = word.translate(remove)
            print(word.lower())

This will print our the lower cased and stripped words, one per line. Not really sure if that's what you want.

Upvotes: 1

Related Questions