0

I'm trying to make pandas dataframe using my CSV file.

Here is my code:

import requests, re, pandas, csv
from bs4 import BeautifulSoup
from io import StringIO

base_url="http://www.hltv.org/?pageid=188&statsfilter=2816&offset="
with open('cs_data1.csv', 'w', newline='') as out_file:
    for page in range(0,1200,50):
        r=requests.get(base_url+str(page))
        c=r.content

        table=BeautifulSoup(c,"html.parser")
        for row in table.find_all('div', style=re.compile(r'width:606px;height:22px;background-color')):
            buffer=StringIO(row.get_text(strip=True, separator=','))
            reader=csv.reader(buffer, skipinitialspace=True)        
            writer=csv.writer(out_file)
            writer.writerows(reader)

That code makes the CSV file and it works fine. Then I try to make pandas dataframe:

df=pandas.read_csv("cs_data1.csv")
df

And there I got the error: "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 22: invalid start byte".

What I should try to encode/decode so the dataframe would work?

1 Answer 1

1

Did you try:

df = pandas.read_csv("cs_data1.csv", encoding='utf-8')
Sign up to request clarification or add additional context in comments.

2 Comments

It works now! Thank you very much :) It's funny how little detail affect to result.
Simple yet effective :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.