Here is my code:
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))
On line df = pd.read_csv... I have this error: 'utf-8' codec can't decode bytes in position 15-16: invalid continuation byte
I searched on other stackoverflow posts but none of them help. Can someone help me?
tripleee
192k37 gold badges319 silver badges370 bronze badges
-
1did u try stackoverflow.com/a/30470630/6660638Epsi95– Epsi952021年08月23日 04:01:07 +00:00Commented Aug 23, 2021 at 4:01
-
1That says your CSV file has international characters and is not in a UTF-8 encoding. Either that, or it's not a text file at all. What language is your CSV file?Tim Roberts– Tim Roberts2021年08月23日 04:01:07 +00:00Commented Aug 23, 2021 at 4:01
-
1Does this answer your question? UnicodeDecodeError, invalid continuation byte. A simple internet search for your error results in myriad answers/options. What exactly have you tried, and what doesn't work?BruceWayne– BruceWayne2021年08月23日 04:02:14 +00:00Commented Aug 23, 2021 at 4:02
-
@Epsi95 : If I use those encoding, there will be an error: list.remove(x): x not in list on the line: df2.remove("Keywords").John– John2021年08月23日 04:08:31 +00:00Commented Aug 23, 2021 at 4:08
-
@Tim Roberts : In my CSV it contains Vietnamese languageJohn– John2021年08月23日 04:08:36 +00:00Commented Aug 23, 2021 at 4:08
1 Answer 1
Vietnamese is Windows code page 1258. So, you likely need this:
df = pd.read_csv("keyword_list_v1.csv", names=colnames, encoding="windows_1258")
answered Aug 23, 2021 at 17:15
Tim Roberts
55.3k4 gold badges29 silver badges41 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-py