Topic: Problem with Python 3.1 vs 2.7
Dear people,
I'm very new to programming in general. I started to teach myself some python 2.7 a few weeks ago, but have switched to 3.1 recently because apparently it is easier to deal with unicode there.
What I want to do is the following:
I have a list of words (utf-8 encoded), and want my program to search this list, line by line, for the occurrence of a certain character (in the example: '3'). When this letter is encountered, I want it to print that character plus the character(s) following it to a file.
Whether I want just the one letter to the right or two to the right of '3' depends on whether that 2nd one is a colon or something else. This is a sample from my word list:
kof:3rE
p:äx
ropot:3
f3t:ruKEI expect the following output:
3r
3
3t:And this is the program I wrote:
try:
fhand = open('/home/***/testdict-r3env')
writeto = open('/home/***/test-r3env.out', 'w')
except:
print('could not open file')
(exit)
for line in fhand:
pos = 0
length = len(line)
while pos < length:
letter = line[pos]
# check for required character
# this seems to be the problem.
if letter is '3':
# see if the character following '3' is itself followed by ':'
# if so, assign the two characters following 3 to the variable env
if line[pos+2:pos+3] is ':':
env = line[pos:pos+3] + '\n'
# if the character following '3' is NOT followed by ':'
# assign just the one character following '3' to the variable env
else:
env = line[pos:pos+2] + '\n'
writeto.write(env)
pos = pos + 1This worked (almost) perfectly fine in python2.7. The only problem I had was that non-ascii characters (ä, ö, ü) where distorted, and some of them were returned as two characters, thus changing the outcome.
In python 3.1 the program shows no errors, but it doesn't write anything to the output either. I tried adding print statements in several positions to see what it does and what it doesn't do, and it seems that it never enters the if-loop that checks whether the variable 'letter' is '3'.
Why is that?
I'm grateful for any help!