Python Excel traditional Conversion

Source: Internet
Author: User

#下载2个文件https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py https:// raw.githubusercontent.com/skydark/nstools/master/zhtools/zh_wiki.py
Import xlrdfrom sqlalchemy import create_engine, metadatafrom sqlalchemy.ext.automap import Automap_basefrom Sqlalchemy.orm Import sessionfrom langconv import *def readexcel (path): WorkBook = xlrd.open_workbook (path) names =    Workbook.sheet_names () print (' All table name of Excel ', names) # Get current table by index Firstsheet = workbook.sheet_by_index (0) # Get first table Print (' Current sheet name:%s '% firstsheet.name) print (' Total rows of the current table:%s '% firstsheet.nrows) print (' Number of columns occupied by current table:%s '% Firstshee T.ncols) # Gets the first row of value # firstrow = firstsheet.row_values (0) # print (' Table first row (column name):%s '% firstrow) for num in RA Nge (firstsheet.nrows): if num > 0:rows = firstsheet.row_values (num) for index, value in E                        Numerate (rows): If index = = 1:if str (value) and Len (str (value)) = = 8: Yield str (value), rows[2] Continueret = Readexcel ("c:/users/kaige/desktop/$ four Yu Yu/new product record-tariff code/mainland tax (2018 to adjust the table with Taiwan tax). xls ") # Traditional to simplified def TRaditional2simplified (sentence): "The traditional characters in sentence to simplified" "sentence = Converter (' Zh-hans '). Convert (sentence) Return sentence# data connection engine = Create_engine (' Mssql+pymssql://username:password/dbname?charset=utf8 ', Echo=False) # ORM Mapping entity class Metadata_r = MetaData () metadata_r.reflect (engine, only=[' base_custtax_copy1_2 ']) Base_r = Automap_base ( Metadata=metadata_r) base_r.prepare () base_custtax_copy1_2 = base_r.classes.base_custtax_copy1_2# Get session Instance session = Session (engine) for code, name in RET: # Add Object base = Base_custtax_copy1_2 (Cust_code=code,cmdt_de    Sc=traditional2simplified (name)) Session.add (base) # Submit Session.commit () # Traditional to Simplified def simplified2traditional (sentence): "Convert the simplified characters in sentence to traditional characters" sentence = Converter (' zh-hant '). Convert (sentence) return sentence

  

Original: Portal

Python Excel traditional Conversion

Related Article

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.