我有一个如下所示的 .csv:
Name Gender DOB Status
A M 12/13/79 Expired
B F 10/25/75 Undocumented
我正在尝试使用 selenium 将每条记录更新到网站。
import pandas as pd
import numpy as np
rec = pd.read_table('oldclients.csv', sep=',')
count_row = people.shape[0]
print (count_row)
s=people.loc[0].tolist()
到目前为止,我已经能够使用 s[1] s[2]
将变量传递给 selenium 来更新网页,但我只让它适用于一行。
我的逻辑是
Count Number of rows in csv
Foreach Row in csv -> pass values
Selenium-> Insert said values in webpage
最佳答案
您可以通过多种方式解决此问题:
1。手动循环行
如果这是您想要做的,您需要使用pd.DataFrame.iterrows()
。
for row in people.iterrows():
#your code here
2。将函数应用于 DataFrame
这是我推荐的,因为它感觉更适合 pandas。
def func(row):
#This function takes in a row and does something with Selenium
#and does not return anything.
people.apply(func, axis=1)
#^This applies the function to each row but does not actually change people.
关于Python 计算 CSV 中的行数并将每行传递给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53088549/