Google sheet的“值输入”选项不会删除单元格中的撇号

2024-09-30 08:28:45 发布

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

我一直在从事一个项目,该项目需要对数据进行清理,并将数据发送到google sheets。然而,发送的记录在其开头有一个撇号,如下面的示例,这是它在谷歌表单中的外观

This is the date value with an apostrophe

这是带撇号的日期值

This is the number value with an apostrophe

这是带撇号的数值

我用value_input_option = 'USER_ENTERED'试着去掉撇号,但没能使它起作用。下面是我的代码示例

import requests
from bs4 import BeautifulSoup
import time
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from google.oauth2.service_account import Credentials
import pandas as pd
index = 818
url = ["https://www.investing.com/indices/us-spx-500-historical-data", 'https://www.investing.com/commodities/gold-historical-data', 'https://www.investing.com/indices/investing.com-btc-eur-historical-data', 'https://mojracun.incrementum.si/chartv2/public']

header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36'}

while True:

page1 = url[0]
page2 = url[1]
page3 = url[2]
page4 = url[3]

pagebtc = requests.get(page3, headers=header)

btc = BeautifulSoup(pagebtc.content, 'html.parser')

btcresult = btc.find(id='curr_table')

btccontent = btcresult.find_all('td')
refdate = btccontent[0].get_text() # Reference all asset dates to this
day = pd.to_datetime(refdate).day 
month = pd.to_datetime(refdate).month
year = pd.to_datetime(refdate).year
today = str(month) + '/' + str(day) + '/' + str(year) # Will be saved to google sheets



btcdate = []
btcprice = []
btcopen = []
btchigh = []
btclow = []
btcvol = []
btchange = []

k = 1
for each in btccontent:
    if k%7 == 1:
        btcdate.append(each.text.strip())
    if k%7 == 2:
        btcprice.append(each.text.strip())
    if k%7 == 3:
        btcopen.append(each.text.strip())
    if k%7 == 4:
        btchigh.append(each.text.strip())
    if k%7 == 5:
        btclow.append(each.text.strip())
    if k%7 == 6:
        btcvol.append(each.text.strip())
    if k%7 == 0:
        btchange.append(each.text.strip())
    k += 1
print(btcdate[0] + ' ' + btcprice[0] + ' ' + btchange[0])


btprice = btcprice[0]
btchange = btchange[0]

row = [today, btprice, btchange]
print(row)
    
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('stock-0e7acf94311d.json', scope)
client = gspread.authorize(creds)
sheet = client.open("Stock Trading Chart")
sheet1 = sheet.worksheet("Sheet1")
sheet1.add_rows(index)
sheet1.insert_row(row, index, value_input_option='USER_ENTERED')
#sheet1.
index += 1
time.sleep(10)

如果你知道为什么会这样,请引导我。请注意,下面的“while”循环代码是缩进的


Tags: totextfromhttpsimportcomurlindex

热门问题