Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 5 - File of Patterns - '\n' Included in Output #67

Open
AdamRogerson97 opened this issue May 3, 2020 · 1 comment
Open

Chapter 5 - File of Patterns - '\n' Included in Output #67

AdamRogerson97 opened this issue May 3, 2020 · 1 comment

Comments

@AdamRogerson97
Copy link

AdamRogerson97 commented May 3, 2020

Hi Mark and All,

I've been working through your book and it has been going really well. Thank you for the clear instruction, witty humour, and clear communication.

I was wondering if you could explain an issue I am having with the File of Patterns example.

Code:
`import re

def build_match_and_apply_functions(pattern, search, replace):
def matches_rule(word):
return re.search(pattern, word)
def apply_rule(word):
fixed = re.sub(search, replace, word)
if
return fixed[:-1]
return (matches_rule, apply_rule)

rules = []
with open('plural4-rules.txt', encoding='utf-8') as pattern_file:
for line in pattern_file:
pattern, search, replace = line.split(None, 2)
rules.append(build_match_and_apply_functions(
pattern, search, replace))

def plural(noun):
for matches_rule, apply_rule in rules:
if matches_rule(noun):
return apply_rule(noun)`

Data File:
[sxz]$ $ es
[^aeioudgkprt]h$ $ es
[^aeiou]y$ y$ ies
$ $ s

Shell Output:

IN: MakePlural.plural('match')
OUT: 'matches\n'

IN: MakePlural.plural('tax')
OUT: 'taxes\n'

IN: MakePlural.plural('happy')
OUT: 'happies\n' *Yes I know this is not a real word.

IN: MakePlural.plural('tent')
OUT: 'tents'

So, the output is including the next line character ('\n') when it uses the data file. But only for the first three rules, it doesn't have this issue for the catch all last rule. The code works perfectly fine when it was the more basic version (having the patterns as strings within the code), but has issues when using the text file. I have tried tabs, spaces, not that it should matter, but it still includes the '\n. I am on a Mac and used text editor to make the text file.

Would love to figure out this mystery. Cheers,

Adam Rogerson

  • Update, Thanks! First time poster, used the <> (still isn't showing the tabbing, not sure how to use it properly) and implemented the .strip. Didn't know it read the \n.
@davipo
Copy link

davipo commented May 3, 2020

(Please use the code formatting tool (<> above), it is difficult to read Python without indenting.)

Reading a file with for line in pattern_file, Python includes a line-end character in line. This ends up in replace, and in your output. I'm guessing you don't have a newline at the end of your pattern file, so the last pattern doesn't end in \n.

To correct the problem, you can do line = line.strip() before the split.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants