大查询编写日语(utf8)用于

2024-04-23 16:18:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经尝试在GCP上使用selenium从web站点获取数据,通过print()命令获取数据并在屏幕上显示这些数据是很好的。但是,当尝试使用to_gbq将数据发送到大查询时,出现以下错误,结果是空白数据。 错误消息:

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 14-30: 
Body ('尚道館・西郷派大東流合気武術総本部') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

该网站是一个日文网页。为了解决这个问题,我使用了str.encode(utf-8)命令,通过从print()查看,字符串似乎被正确地转换为utf-8。然而,它得到了同样的错误。 我的环境是GCP上的Python3

 data_rows = []
    for e in elems: 
        data = e.text
        data_row = data.splitlines()
        data_row.append(datetime.datetime.now())
        if len(data_row) == 6:
            data_row[0] = data_row[0].encode('utf-8')
            data_row[1] = data_row[1].encode('utf-8')
            data_row[2] = data_row[2].encode('utf-8')
            data_row[3] = data_row[3].encode('utf-8')
            data_row[4] = data_row[4].encode('utf-8')
            data_rows.append(data_row)
    driver.quit()
    sample_dataframe = pd.DataFrame(data_rows,columns= 
    ['shop_name','category','nearest_station','telephone_number','address','DL_time'])
    print(sample_dataframe)

    sample_dataframe.to_gbq('NTT.aikidou2025', 'robotic-column-270803',if_exists = 'replace')`

Tags: to数据samplein命令dataframedataif