Alex
Alex

Reputation: 13

Reading ε using python

So I am trying to read the following line from the source file called 'bad_grammar_4.txt':

B -> ε
A -> ε

The code I used read in the above file, and i tried to print it out , but it produces the following output

A -> ε
B -> ε

Since I want to use my code to do something whenever there is a ε in the line. But I couldn't figure out what should I use in my if statement in order to capture the existence of ε. It seems like python just doesn't recognise ε at all....

Can anyone show me how should ε be recognised ?

This is the Code i used to print the file out directly:

with open('bad_grammar_4.txt', 'r') as file:
        for line in file:
            print(line)

Upvotes: 0

Views: 153

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799390

The encoding of the file has been guessed incorrectly:

with open('bad_grammar_4.txt', 'r', encoding='utf-8') as file:
   ...

Or with Python 2.x:

with io.open('bad_grammar_4.txt', 'r', encoding='utf-8') as file:
   ...

Upvotes: 2

Sait
Sait

Reputation: 19855

You can check if a string contains a substring like:

>>> 'o' in 'hello'
True
>>> 'el' in 'hello'
True
>>> 'x' in 'hello'
False
>>> 'hex' in 'hello'
False

You can find how Python encode a character like:

>>> 'ε'
'\xce\xb5'
>>> 'π'
'\xcf\x80'
>>> 'ρ'
'\xcf\x81'

So, we know ε is encoded as \xce\xb5. Then you can basically:

with open('a.txt') as f:
   for line in f:
      if '\xce\xb5' in line:
          print(line)

Upvotes: 1

Related Questions