当我从Excel中使用def函数提取数据在python中,我得到的单引号和双引号(“”,'',[])输出

问题描述:

当我从Excel中提取数据使用def功能在python硒,我我在单引号和双引号中输出(“”,“',[])。我不想在单引号或双引号中输出。当我从Excel中使用def函数提取数据在python中,我得到的单引号和双引号(“”,'',[])输出

另外,当我从使用硒的“Gmail应用程序”中的Excel工作表读取数据时,它会读取单引号和双引号。

你们可以帮我解决这个问题吗?

from selenium import webdriver 
from time import sleep 
from xlrd import open_workbook 
import xlrd 
import xlwt 



book=xlrd.open_workbook("E:\\Email.xlsx") 
print(book.nsheets) 
SheetName =book.sheet_by_name("gmail") 
rows=SheetName.nrows 
cols=SheetName.ncols 
def getcell(rows,cols): 

    table=list() 
    record=list() 

    for x in range(rows): 
     for y in range(cols): 
      record.append(SheetName.cell(x,y).value)  
      table.append(record) 
      record=[] 
      rows=rows+1 

    return table; 

v=getcell(1,1) 
print(str(v)) 

OUTPUT:

[ 'ABCDEFG']]

[ '[email protected]']

driver = webdriver.Chrome("C:\\Users\\home\\workspace\\geeko\\chromedriver.exe") 
driver.maximize_window() 
driver.get("https://www.gmail.com/") 
elem=driver.find_element_by_xpath("//*@id='identifierId']").send_keys(str(getcell(1, 1))) 
+0

你可以尝试格式化你的代码替换

elem=driver.find_element_by_xpath("//*@id='identifierId']").send_keys(str(getcell(1, 1))) 

?另外,您能否准确地说出您从电子表格中阅读的内容以及您想要的期望值? – Tom

+0

我想输出“[['abcdefg']] [['[email protected]']]没有单引号或括号 – CPP

+0

我的意思是我得到输出”[['abcdefg']] [['abc @ gmail.com']]与单引号和括号。我想要没有单个,双引号和括号的o/p – CPP

确定添加此功能,您的代码(注意它会将表连接在一个字符串中,但您可以使用您需要的任何分隔符对其进行修改

def strip_table(t): 
    out_str = "" 
    for item in t: 
     out_str += str(item[0]) 

    return out_str 

然后用

elem=driver.find_element_by_xpath("//*@id='identifierId']").send_keys(strip_table(getcell(1, 1))) 
+0

不工作。请帮助球员 – CPP

+0

CPP我刚刚更新了答案 – Dman

+0

nope没有工作。通过添加上面的代码,传递给gmail文本框的数据是null.Also也不会抛出任何错误消息。 – CPP