【发布时间】:2020-11-27 13:06:37
【问题描述】:
我正在尝试从 excel 中打印值,并且值是数字。我的目标是阅读这些值并在谷歌中一一搜索。当值为 'nan' 时将停止 x 秒,然后跳过此 'nan' 并继续前进到下一个。
面临的问题:
- 它正在以科学计数法格式打印出来
- 想要在 excel 中出现“nan”时停止做某事
- 将 UPC[i] 复制到谷歌搜索中,但我只想复制一次,因为我想设计它打开新标签然后复制第二个 UPC[i]
我的解决方案:
- 我在 set_option 中有 'lambda x: '%0.2f' % x' 以使其打印出 xxxxxx.00 并带有 2 个小数。即使我想要 int 格式,但它已经比科学记数法格式更好
- 使用 'if' 查看 upc[i] 中的值是否等于 'nan'
- 我现在想不起来
代码:
import pandas as pd
import numpy as np
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import msvcrt
import datetime
import time
driver = webdriver.Chrome()
#Settings
pd.set_option('display.width',10, 'display.max_rows', 10, 'display.max_colwidth',100, 'display.width',10, 'display.float_format', lambda x: '%0.2f' % x)
df = pd.read_excel(r"BARCODE.xlsx", skiprows = 2, sheet_name = 'products id')
#Unnamed: 1 is also an empty column, i just didn't input UPC as title in excel.
upc = df['Unnamed: 1']
#I can't print out as interger...It will always have a xxxxx.0
print((upc[0:20]))
count = len(upc)
i = 0
for i in range(count ):
if upc[i] == 'nan':
'skip for x seconds and continue, i am not sure how to do yet'
else:
print(int(upc[i]))
driver.get('https://www.google.com')
driver.find_element_by_name('q').send_keys(int(upc[i]))
i = i + 1
打印出来:
3337872411991.0
3433422408159.0
3337875598071.0
3337872412516.0
3337875518451.0
3337875613491.0
3337872413025.0
3337875398961.0
3337872410208.0
nan <- i want program to stop here so i can do something else.
3337872411991.0
3433422408159.0
3337875598071.0
3337872412516.0
3337875518451.0
3337875613491.0
3337872413025.0
3337875398961.0
3337872410208.0
nan
Name: Unnamed: 1, Length: 20, dtype: float64
3337872411991
3433422408159
3337875598071
3337872412516
3337875518451
etc....
在 Google 上搜索了一些关于数字的格式,例如设置打印格式,但我对 .format 和 lambda 感到困惑。
【问题讨论】:
标签: python-3.x excel pandas selenium