I was going to read from a file or standard input so wrote below code. Reads from file by fileinput(filename) when Debug is true and fileinput() if not. Strange thing is, when i print 'line', it prints well on output file. However, when i get a char from the line by index or inspect the line on debug break, it appears like "癤풵UBWEWUB" while correct input is "WUBWEWUB" (It is input file content).
import os
import sys
import psutil
import fileinput
if __debug__:
Debug = True
import defs
else: Debug = False
if(Debug == True):
def memory():
pass
inputFilePath = os.path.join(defs.IO_Dir, "input.txt")
inf = open(inputFilePath, "r")
outf = open(os.path.join(defs.IO_Dir, "output.txt"), "w")
logf = open(os.path.join(defs.IO_Dir, "log.txt"), "a")
logf.write(f"Program started at : {gettime()}\n")
def write(str):
print(str, file = outf)
pass
inval = inputFilePath
sys.stdout = outf
pass
else:
inval = None
pass
if(Debug): print("Start------------------------")
for x in fileinput.input(inval):
line = x.strip()
if(line == "Exit"):
break
ans = ""
i = 0
l = 3
print(type(line))
print(type(line[0]))
for i in range(2, len(line) - 3):
if(line[i] == 'W' and line[i+1] == 'U' and line[i+2] == 'B'):
if(i + 3 == len(line)):
break
pass
l = i + 3
if(ans[-1] != ' '):
ans = ans + " "
pass
elif(i >= l):
ans = ans + line[i]
pass
pass
pass
print(ans)
print(line)
if(Debug): print("End------------------------")
output file:
Start------------------------
<class 'str'>
<class 'str'>
BWE
WUBWEWUB
End------------------------
I expected it as encoding problem, but the type of line is just 'str' and i can't find why strange letter is there.