Python+selenium Excel Chinese read fill to Web page

Source: Internet
Author: User

Because of my technical level, grinding haw for a long time finally came out, in the finishing time thinking, Maybe I was too concerned about the printed list, list can not display chinese, I thought I was always wrong.

Attach form part

#Coding:utf-8ImportxlrddefOpen_excel (file):Try: Data=Xlrd.open_workbook (file)returnDataexceptException as E:Print(str (e))defExcel_table_byindex (file,colnameindex = 0,by_index=0): Data=open_excel (file) Table=data.sheets () [by_index] nrows=table.nrows#LineColnames =table.row_values (colnameindex)#Column Contentslist=[]     forRowNuminchRange (1, nrows): Row=table.row_values (rownum)ifRow:app= {}             forIinchRange (len (colnames)): s=row[i]
#if isinstance (s, unicode): #s = Row[i].encode ("utf-8") Print "each one", S app[colnames[i]]=s List.append (app)PrintRowPrintListreturnListlistdata=excel_table_byindex ("E:\py\\alwaystry\sign_up.xlsx", 0)

Added at the beginning  , it is already in the Utf-8 format, so can handle chinese.
Print  " each"
Row prints out is [u ' \u4e8c\u6bdba ',...
The list print is [{u ' nickname ': u ' \u4e8c\u6bdba ' ..., all rows of Data.
I only saw the original Unicode encoding that he could not print chinese, in fact, the list can not be Displayed. It's still too tender to Come.

In the long quest, I know the STR and Unicode

(i look at the URL that feels good http://wklken.me/posts/2013/08/31/python-extra-coding-intro.html)

anyway, Yes.

Str-decode (' STR encoded format '), Unicode

Unicode-encode (' you want to format ') str

To view the encoding format, print chardet.detect (listdata[i][' nickname ') to determine the character encoding format of course or import Chardet

He sometimes reported valueerror:expected a bytes object, not a Unicode object wrong, and that would know what encoding format he Was.

still, the direct printing format: print type (listdata[i][' realname ')) printout <type ' Unicode ', which means that someone else is already unicode, Then you just have to delete the Chardet.detect ().

UTF-8 is one of the ways Unicode is Implemented/utf8 is an encoding of the Unicode character set. ( When I took notes, I thought the two words were quite understandable.)

if, very careless, you went to the same situation as I did, early to the encode, is commented out of the two Sentences.

Tip unicodedecodeerror: ' UTF8 ' codec can ' t decode byte 0xe4 in position 0:unexpected end of data and so on, may wish to try Decode. If the Drop-down box matches the situation, you can first decode again encode try, the following line of code Last.

The overall approximate code is as follows

#Enter URLAddress = Raw_input ('Enter location:')ifLen (address) < 1:    Print "Error"URL=Address#put your configuration fileProfile_dir = R"C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\5cmfbcqp.default" profile=Webdriver. Firefoxprofile (profile_dir) Driver=Webdriver. Firefox (profile)#Open ExceldefOpen_excel ():Try: Data=Xlrd.open_workbook (file)returnDataexceptException as E:Print(str (e))defExcel_table_byindex (file,colnameindex = 0,by_index=0): Data=open_excel (file) Table=data.sheets () [by_index] nrows=table.nrows#LineColnames =table.row_values (colnameindex)#Column Contentslist=[]     forRowNuminchRange (1, nrows): Row=table.row_values (rownum)ifRow:app= {}             forIinchRange (len (colnames)):s =row[i]ifisinstance (s, unicode): s= Row[i].encode ("Utf-8")                Printsapp[colnames[i]] =s List.append (app)Print(list)returnList#You can also use the if else to determine whether there is a CSSdefjudgewithcss (css):Try: Driver.find_element_by_css_selector (css)except:        returnFalsereturnTruelistdata=excel_table_byindex ("E:\sign_up.xlsx", 0)if(len (listdata) <0):assert0,u"Data Exceptions" forIinchrange (0, len (listdata)): driver.get (url)#nickname, This is an input    ifJUDGEWITHCSS ("#nickname") ==True:nickname= driver.find_element_by_id ("Nickname") nickname.clear ()PrintChardet.detect (listdata[i]['Nickname']) Nickname.send_keys (listdata[i]['Nickname'].decode ("Utf-8"))    #Blood type, This is a select    ifJUDGEWITHCSS ("#blood") ==True:blood= driver.find_element_by_id ("Blood")        #The following 3 types can also be judged by the format        #print isinstance (listdata[i][' blood '), unicode)        #print isinstance (listdata[i][' blood '), str)        #print type (listdata[i][' blood ')        PrintChardet.detect (listdata[i]['Blood']) Select (blood). select_by_value (listdata[i]['Blood'].decode ("windows-1252"). Encode ("windows-1252"))

Python+selenium Excel Chinese read fill to Web page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.