-1

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
asked Aug 23, 2021 at 3:58
7
  • 1
    did u try stackoverflow.com/a/30470630/6660638 Commented Aug 23, 2021 at 4:01
  • 1
    That 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? Commented Aug 23, 2021 at 4:01
  • 1
    Does 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? Commented 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"). Commented Aug 23, 2021 at 4:08
  • @Tim Roberts : In my CSV it contains Vietnamese language Commented Aug 23, 2021 at 4:08

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.