我有一个使用 Pandas 创建的 CSV 文件。下面是以下代码的输出:
test = pd.read_csv('order.csv', header=0)
print(test.head())
3 16258878505032
0 3 16258876670024
1 3 16258876899400
2 3 16258876997704
我唯一需要处理的数据是第2列中的信息和第3列中的信息。这是采购订单数据,其中第二列代表“数量”,第三列代表“sku”。
我需要获取第 1 行第 2 列并使用 selenium 将其注入(inject)到输入字段中。我需要第 1 行第 3 列并执行在网页上选择 sku 的操作。将商品添加到购物车并循环返回流程第 2 行、第 3 行等。
我知道如何编写 selenium 代码来执行基于 Web 的操作,但不确定如何编写 pandas/python 代码来一次一行地迭代 CSV 文件以及如何调用这些值。我的逻辑如下。
read order.csv
get quantity value and sku value for row (one row at the time)
visit website, inject quantity value
remain on website, select sku
add to cart
repeat loop until no more rows to process
感谢您的帮助。
最佳答案
首先使用read_csv
中的参数names
为了避免将第一行数据转换为列名称:
test = pd.read_csv('order.csv', names=['quantity','sku'])
print (test)
quantity sku
0 3 16258878505032
1 3 16258876670024
2 3 16258876899400
3 3 16258876997704
因为可以使用selenium
和网络使用DataFrame.iterrows
或其他循环解决方案:
def func(x):
q = x['quantity']
sku = x['sku']
print (q, sku)
#add selenium code
df.apply(func, axis=1)
或者:
for i, row in test.iterrows():
q = row['quantity']
sku = row['sku']
print (q, sku)
#add selenium code
关于python - 使用 Pandas 迭代 CSV 行,执行 Selenium 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55091244/