gpt4 book ai didi

python - Pandas read_fwf 特殊字符未正确加载

转载 作者:行者123 更新时间:2023-11-28 18:25:44 25 4
gpt4 key购买 nike

所以我在 test.txt 中有以下数据:

étoufee
placing

和下面的代码:

import pandas as pd
import numpy as np

widths = [4,3]
names = ["part1", "part2"]

df = pd.read_fwf('test.txt',widths = widths, names = names, encoding = 'utf8')
print df

输出是:

  part1 part2
0 éto ufe
1 plac ing

注意第一行。特殊字符导致 read_fwf 正确读取长度,我们正在丢失数据。我试过设置 encoding = utf-8 但这没有用。还有其他选择吗?


对于那些将来可能会看到这个的人,这是更新后的代码

# encoding=utf8

import pandas as pd
import numpy as np
from io import StringIO
import sys, locale
import codecs


with codecs.open('test.txt','r',encoding='utf8') as f:
text = f.read()



widths = [4,3]
names = ["part1", "part2"]

df = pd.read_fwf(StringIO(text),widths = widths, names = names, encoding = 'utf8')
print(df)

最佳答案

不是答案
可能有帮助

txt = """étoufee
placing"""

import pandas as pd
import numpy as np
from io import StringIO

widths = [4,3]
names = ["part1", "part2"]

df = pd.read_fwf(StringIO(txt),widths = widths, names = names, encoding = 'utf8')
print(df)

part1 part2
0 étou fee
1 plac ing

import sys, locale
print(sys.version)
print(pd.__version__)
print(sys.getfilesystemencoding())
print(sys.getdefaultencoding())
print(locale.getlocale())

3.5.2 |Anaconda custom (x86_64)| (default, Jul 2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)]
0.19.0
utf-8
utf-8
('en_US', 'UTF-8')

关于python - Pandas read_fwf 特殊字符未正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41581322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com