'decimal' codec can't encode character u'\ufeff' in position 0: invalid decimal Unicode string

这是由于txt文件保存方式是 UTF-8-BOM 而导致的
utf-8保存格式的txt文件encoding方式就是 encoding='utf-8'
utf-8-BOM保存格式的txt文件encoding方式是 encoding="UTF-8-SIG"
【'decimal' codec can't encode character u'\ufeff' in position 0: invalid decimal Unicode string】所以只要改成:with io.open(filepath,'r',encoding="UTF-8-SIG") as f: 就没有问题了

    推荐阅读