【发布时间】:2019-04-13 11:40:15
【问题描述】:
我有一个 CSV 文件,其中包含通过 API 下载的一些推文。推文包含一些 Unicode 字符,我很清楚如何解码它们。
我把CSV文件放到DataFrame中,
df = pd.read_csv('sample.csv', header=None)
columns = ['time', 'tweet']
df.columns = columns
其中一条推文是 -
b'RT : This little girl dressed as her father for Halloween, a employee \xf0\x9f\x98\x82\xf0\x9f\x98\x82\xf0\x9f\x91\x8c (via )'
但是当我通过命令访问这条推文时 - df['tweet'][0]
输出以以下格式返回 -
"b'RT : This little girl dressed as her father for Halloween, a employee \\xf0\\x9f\\x98\\x82\\xf0\\x9f\\x98\\x82\\xf0\\x9f\\x91\\x8c (via ) '"
我无法弄清楚为什么这个额外的反斜杠会附加到推文中。因此,该内容不会被解码。以下是 DataFrame 中的几行。
time tweet
0 2018-11-02 05:55:46 b'RT : This little girl dressed as her father for Halloween, a employee \xf0\x9f\x98\x82\xf0\x9f\x98\x82\xf0\x9f\x91\x8c (via )'
1 2018-11-02 05:46:41 b'RT : This little girl dressed as her father for Halloween, a employee \xf0\x9f\x98\x82\xf0\x9f\x98\x82\xf0\x9f\x91\x8c (via )'
2 2018-11-02 03:44:35 b'Like, you could use a line map that just shows the whole thing instead of showing a truncated map that\xe2\x80\x99s confusing.\xe2\x80\xa6 (via )
3 2018-11-02 03:37:03 b' service is a joke. No service northbound No service northbound from Navy Yard after a playoff game at 11:30pm. And they\xe2\x80\xa6'
正如我之前提到的,如果直接访问这些推文中的任何一条,都会在输出中附加一个额外的反斜杠。
谁能解释一下为什么会发生这种情况以及如何避免它?
谢谢
【问题讨论】:
-
显示原始 .CSV 中的一些示例行。好像一开始就写错了。如果您编写了 CSV,您可能会提出一个新问题,即如何从 API 中读取并将其正确写入 CSV。这看起来像 XY Problem。
标签: python-3.x pandas dataframe twitter unicode