我的第一列名称中有不需要的字符。 这些字符在excel、记事本和sublime中不可见
我尝试了here中的提示来检查列名。只有这样,才能看到不需要的字符
有什么好的解决办法吗
M1
Out[347]:
a1 b1 a2 b2
0 0.238066 0.976816 0.238066 0.976816
1 0.373340 1.469728 0.373340 1.469728
2 0.968814 1.248595 0.968814 1.248595
3 0.886586 3.451292 0.886586 3.451292
4 0.244301 2.206757 0.244301 2.206757
5 0.389688 2.893761 0.389688 2.893761
6 0.704340 2.621483 0.704340 2.621483
7 0.301238 1.678316 0.301238 1.678316
8 0.375927 0.574135 0.375927 0.574135
9 0.065749 2.259736 0.065749 2.259736
print(M1.columns.tolist())
['\ufeffa1', 'b1', 'a2', 'b2']
M1.columns = M1.columns.str.strip().str.lower().str.replace(' ', '_').str.replace('(', '').str.replace(')', '')
print(M1.columns.tolist())
['\ufeffa1', 'b1', 'a2', 'b2']
这就是编码问题
请使用'Some String'。编码('ascii','ignore'),它给出 字节,并使用解码来获取字符串
代码:
输出:
字符
\ufeef
(U+FEFF)是一个byte order mark (BOM),它是一个特殊的字符,通知读者编码的“端性”(小端对大端)。对于utf-8,BOM是可选的,通常不会写入。您可能正在使用默认编码读取带有BOM的UTF-8文件,即“UTF-8”(不带BOM的UTF-8)。尝试使用“utf-8-sig”(带BOM的utf-8)编辑:“那么我应该如何处理文件?更改编码并不能解决问题。”
您可以打开记事本++,然后格式化->;转换为UTF-8。或者在Python中:
这将删除BOM表
相关问题 更多 >
编程相关推荐