-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
  • 175,061
  • 34
  • 275
  • 318
John
  • 49
  • 1
  • 2
  • 6
  • 1
    did u try https://stackoverflow.com/a/30470630/6660638 – Epsi95 Aug 23 '21 at 04: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? – Tim Roberts Aug 23 '21 at 04:01
  • 1
    Does this answer your question? [UnicodeDecodeError, invalid continuation byte](https://stackoverflow.com/questions/5552555/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 Aug 23 '21 at 04: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 Aug 23 '21 at 04:08
  • @Tim Roberts : In my CSV it contains Vietnamese language – John Aug 23 '21 at 04:08
  • You should change the text of the question. – Keiku Aug 23 '21 at 06:53
  • `df2` contains the contents of the "keywords:" column. Why do you think it will contain the word "Keywords"? And if all you want is a list, why on earth are you using the heavyweight pandas module to read your CSV file? Just use the `csv` module. – Tim Roberts Aug 23 '21 at 17:18

1 Answers1

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")
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30