python读取excel送到网页_python+selenium excel中文读取填充到网页
由于我技術(shù)水平,磨磨唧唧很長(zhǎng)時(shí)間終于弄出來(lái)了,在整理的時(shí)候思考了下,可能是我當(dāng)時(shí)太關(guān)注打印出來(lái)的list了 ,list里不能顯示中文,我就初以為我總是錯(cuò)了。
附上表格一部分
#coding:utf-8
importxlrddefopen_excel(file):try:
data=xlrd.open_workbook(file)returndataexceptException as e:print(str(e))def excel_table_byindex(file,colnameindex = 0,by_index=0):
data=open_excel(file)
table=data.sheets()[by_index]
nrows=table.nrows#行
colnames =table.row_values(colnameindex)#列內(nèi)容
list=[]for rownum in range(1, nrows):
row=table.row_values(rownum)ifrow:
app={}for i inrange(len(colnames)):
s=row[i]#if isinstance(s, unicode):
#s = row[i].encode("utf-8")
print "每一個(gè)", s
app[colnames[i]]=s
list.append(app)printrowprintlistreturnlist
listdata=excel_table_byindex("E:\py\\alwaystry\sign_up.xlsx",0)
在開頭加了#coding:utf-8,就已經(jīng)是utf-8格式的了,所以能處理中文。
打印print"每一個(gè)", s 的時(shí)候,打印出來(lái)是 每一個(gè) 二毛a
printrow 打印出來(lái)是[u'\u4e8c\u6bdba',...
printlist 打印出來(lái)就是[{u'nickname': u'\u4e8c\u6bdba'..., 所有的行數(shù)據(jù)。
當(dāng)初的我只看到了Unicode編碼就以為他是打印不了中文了,其實(shí)是list不能顯示。現(xiàn)在想來(lái),還是太嫩
在漫長(zhǎng)的探索中,我知道了str和Unicode
(我看著覺(jué)得不錯(cuò)的網(wǎng)址http://wklken.me/posts/2013/08/31/python-extra-coding-intro.html)
反正就是
str -> decode('str的編碼格式') -> unicode
unicode -> encode('你要的格式') -> str
要查看編碼格式,就print chardet.detect(listdata[i]['nickname']),判斷字符編碼格式當(dāng)然還是要import chardet的
他有時(shí)候會(huì)報(bào)ValueError: Expected a bytes object, not a unicode object錯(cuò),那也就知道他是什么編碼格式了。
還是這樣吧,直接打印格式:print type(listdata[i]['realname'])打印輸出,說(shuō)明人家已經(jīng)是unicode,那你就把chardet.detect()刪掉就好。
UTF-8是Unicode的實(shí)現(xiàn)方式之一/utf8是對(duì)unicode字符集進(jìn)行編碼的一種編碼方式。(當(dāng)時(shí)做筆記記下來(lái)的,我覺(jué)得這兩句話還是蠻好理解的)
如果,很不慎,你走到了和我當(dāng)初一樣的境地,早早的把encode,就是注釋掉的那兩句。
提示UnicodeDecodeError: 'utf8' codec can't decode byte 0xe4 in position 0: unexpected end of data等等,不妨嘗試一下decode。如果是下拉框匹配的情況,可以先decode再encode試試,下面代碼最后一行。
整體大概代碼如下
#輸入網(wǎng)址
address = raw_input('Enter location:')if len(address) < 1:print "error"url=address#放你的配置文件
profile_dir = r"C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\5cmfbcqp.default"profile=webdriver.FirefoxProfile(profile_dir)
driver=webdriver.Firefox(profile)#打開excel
defopen_excel():try:
data=xlrd.open_workbook(file)returndataexceptException as e:print(str(e))def excel_table_byindex(file,colnameindex = 0,by_index=0):
data=open_excel(file)
table=data.sheets()[by_index]
nrows=table.nrows#行
colnames =table.row_values(colnameindex)#列內(nèi)容
list=[]for rownum in range(1, nrows):
row=table.row_values(rownum)ifrow:
app={}for i inrange(len(colnames)):
s =row[i]ifisinstance(s, unicode):
s= row[i].encode("utf-8")prints
app[colnames[i]] =s
list.append(app)print(list)returnlist#通過(guò)css判斷是否存在,你也可以用if else
defjudgewithcss(css):try:
driver.find_element_by_css_selector(css)except:returnFalsereturnTrue
listdata=excel_table_byindex("E:\sign_up.xlsx",0)if(len(listdata)<0):assert 0,u"數(shù)據(jù)異常"
for i inrange(0, len(listdata)):
driver.get(url)#昵稱,這是個(gè)input
if judgewithcss("#nickname") ==True:
nickname= driver.find_element_by_id("nickname")
nickname.clear()print chardet.detect(listdata[i]['nickname'])
nickname.send_keys(listdata[i]['nickname'].decode("utf-8"))#血型,這是個(gè)select
if judgewithcss("#blood") ==True:
blood= driver.find_element_by_id("blood")#以下3種也能判斷出格式
#print isinstance(listdata[i]['blood'], unicode)
#print isinstance(listdata[i]['blood'], str)
#print type(listdata[i]['blood'])
print chardet.detect(listdata[i]['blood'])
Select(blood).select_by_value(listdata[i]['blood'].decode("windows-1252").encode("windows-1252"))
總結(jié)
以上是生活随笔為你收集整理的python读取excel送到网页_python+selenium excel中文读取填充到网页的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 乐高机器人教室布置图片大全_圣诞节手抄报
- 下一篇: groovy怎样从sql语句中截取表名_