【问题标题】:Pandas CSV: 'utf-8' codec can't decode bytes in position 15-16: invalid continuation byte [duplicate]Pandas CSV:“utf-8”编解码器无法解码位置 15-16 中的字节:无效的继续字节 [重复]
【发布时间】:2021-08-23 03:58:57
【问题描述】:

这是我的代码:

from pytrends.request import TrendReq
import pandas as pd
import time
startTime = time.time()
pytrend = TrendReq(hl='en-GB', tz=360)

colnames = ["keywords"]
df = pd.read_csv("keyword_list_v1.csv", names=colnames)
df2 = df["keywords"].values.tolist()
df2.remove("Keywords")

dataset = []

for x in range(0,len(df2)):
 keywords = [df2[x]]
 pytrend.build_payload(
 kw_list=keywords,
 cat=0,
 timeframe='2021-08-10 2021-08-23',
 geo='GB')
 data = pytrend.interest_over_time()
 if not data.empty:
      data = data.drop(labels=['isPartial'],axis='columns')
      dataset.append(data)

result = pd.concat(dataset, axis=1)
result.to_csv('search_trends.csv')

executionTime = (time.time() - startTime)
print('Execution time in sec.: ' + str(executionTime))

在线df = pd.read_csv...我有这个错误:'utf-8'编解码器无法解码位置15-16的字节:无效的继续字节

我搜索了其他 stackoverflow 帖子,但没有一个有帮助。有人可以帮我吗?

【问题讨论】:

  • 这表示您的 CSV 文件包含国际字符并且不是 UTF-8 编码。要么,要么根本不是文本文件。您的 CSV 文件是什么语言的?
  • 这能回答你的问题吗? UnicodeDecodeError, invalid continuation byte。对您的错误进行简单的互联网搜索会产生无数的答案/选项。您到底尝试了哪些,哪些不起作用?
  • @Epsi95 :如果我使用那些编码,会出现错误:list.remove(x): x not in list on the line: df2.remove("Keywords").
  • @Tim Roberts:在我的 CSV 文件中,它包含越南语

标签: python python-3.x pandas csv character-encoding


【解决方案1】:

越南语是 Windows 代码页 1258。所以,您可能需要这个:

df = pd.read_csv("keyword_list_v1.csv", names=colnames, encoding="windows_1258")

【讨论】:

    猜你喜欢
    • 2021-08-29
    • 2019-08-07
    • 2014-12-24
    • 1970-01-01
    • 2018-01-11
    • 2020-11-02
    • 2021-01-02
    • 2021-04-27
    • 1970-01-01
    相关资源
    最近更新 更多