Python字符串切片无法按预期工作

时间:2017-06-23 11:47:42

标签: python python-3.6

我打算从文件或标准输入读取,所以写下面的代码。当Debug为true时,从fileinput(filename)读取文件,如果不是则读取fileinput()。奇怪的是,当我打印'line'时,它在输出文件上打印得很好。但是,当我通过索引从行获取char或在调试中断时检查行时,它看起来像“疖풵UBWEWUB”,而正确的输入是“WUBWEWUB”(它是输入文件内容)。

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------------------------")

输出文件:

Start------------------------
<class 'str'>
<class 'str'>
BWE 
WUBWEWUB
End------------------------

我认为它是编码问题,但是线的类型只是'str',我找不到为什么会有奇怪的字母。

1 个答案:

答案 0 :(得分:0)

文本文件有BOM标题,它包含在切片中(line [0],line [1])。我使用visual studio并删除了BOM表头,保存为utf 8而没有签名。