利用Python3将EXCEL中某列特殊字符之前的汉字取首字母,特殊字符之后的汉字取全拼,然后用下划线“_”相连,写入下一列...
把*******.xls中的汉字人名转成用户名,写到后面的单元格中。
例如:网络--李大海 : wl_lidahai
现场-扬帆 : xc_yangfan
蹭课_张马: ck_zhangma
代码如下:
主要实现了汉字转拼音、获取拼音首字母、复杂字符串中提取汉字(Python3 提取中文的正则表达式)以及对excel的读和修改。
#_author:'ZYB' #data:2018/12/27 from xpinyin import Pinyin import re import xlrd from xlutils import copy def single_get_first(unicode):str1 = unicode.encode('gbk')try:ord(str1)return str1except:asc = str1[0] * 256 + str1[1] - 65536if asc >= -20319 and asc <= -20284:return 'a'if asc >= -20283 and asc <= -19776:return 'b'if asc >= -19775 and asc <= -19219:return 'c'if asc >= -19218 and asc <= -18711:return 'd'if asc >= -18710 and asc <= -18527:return 'e'if asc >= -18526 and asc <= -18240:return 'f'if asc >= -18239 and asc <= -17923:return 'g'if asc >= -17922 and asc <= -17418:return 'h'if asc >= -17417 and asc <= -16475:return 'j'if asc >= -16474 and asc <= -16213:return 'k'if asc >= -16212 and asc <= -15641:return 'l'if asc >= -15640 and asc <= -15166:return 'm'if asc >= -15165 and asc <= -14923:return 'n'if asc >= -14922 and asc <= -14915:return 'o'if asc >= -14914 and asc <= -14631:return 'p'if asc >= -14630 and asc <= -14150:return 'q'if asc >= -14149 and asc <= -14091:return 'r'if asc >= -14090 and asc <= -13119:return 's'if asc >= -13118 and asc <= -12839:return 't'if asc >= -12838 and asc <= -12557:return 'w'if asc >= -12556 and asc <= -11848:return 'x'if asc >= -11847 and asc <= -11056:return 'y'if asc >= -11055 and asc <= -10247:return 'z'return '' def GetFirstPinyin(string):if string == None:return Nonelst = list(string)charLst = []for l in lst:charLst.append(single_get_first(l))return ''.join(charLst) def ChoiceHanZi(str):# 中文匹配正则chinese_pattern = '[\u4e00-\u9fa5]+'res = re.findall(chinese_pattern, str)return res if __name__ == '__main__':book = xlrd.open_workbook('*****.xls')new_book = copy.copy(book)sheet = book.sheet_by_index(0)new_sheet = new_book.get_sheet(0) # 修改excel的时候,只能通过get_sheet()for row in range(1,sheet.nrows):res = sheet.row_values(row)[0]Li = ChoiceHanZi(res)hi = GetFirstPinyin(Li[0])+'_'+Pinyin().get_pinyin(Li[1],'')new_sheet.write(row, 1, hi)new_book.save('*****.xls')
转载于:https://www.cnblogs.com/arraon/p/arraon.html
利用Python3将EXCEL中某列特殊字符之前的汉字取首字母,特殊字符之后的汉字取全拼,然后用下划线“_”相连,写入下一列...相关推荐
- 在python将字符串中的空格转换为下划线_如何将下划线替换为空格,反之亦然?...
令人惊讶的是,这个图书馆还没有提到 python包名为python-slugify,它在slugifying方面做得相当不错: pip install python-slugify像这样工作: fro ...
- python中五种下划线 _
python中五种下划线 "_" 单前导下划线:_var 单末尾下划线:var_ 双前导下划线:__var 双前导和末尾下划线:var 单下划线:_ 在文章结尾处,你可以找到一个简 ...
- python 表格格式输出_利用python对excel中一列的时间数据更改格式操作
问题场景:需要将下列的交期一列的数据格式更改成2019/05/10 存货编码 尺寸 数量 交期 0 K10Y0190000X B140 200 2019-05-10 00:00:00 1 K10Y01 ...
- python对excel数据更改_利用python对excel中一列的时间数据更改格式代码示例
本篇文章小编给大家分享一下利用python对excel中一列的时间数据更改格式代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. 问题场景:需要将 ...
- 利用VBA将excel中链接转换为图片形式存放在指定的单元格中
利用VBA将excel中链接转换为图片形式存放在指定的单元格中 VBA代码如下 Private Declare Function URLDownloadToFile Lib "urlmon& ...
- python excel绘图-利用python在excel中画图的实现方法
一.前言 以前大学时候,学EXCEL看到N多大神利用excel画图,觉得很不可思议.今个学了一个来月python,膨胀了就想用excel画图.当然,其实用画图这个词不甚严谨,实际上是利用opencv遍 ...
- Excel中如何获取汉字拼音首字母
Excel中如何获取汉字拼音首字母 在工作中,为了方便查询.排序或者编码,需要生成汉字内容信息的首字母,如果信息很多的话,一个个录入是一件很麻烦的事情,下面给大家介绍一个便捷的方法,轻松获取汉字拼音首 ...
- 在Excel中如何获取汉字拼音首字母?
转自:https://jingyan.baidu.com/article/0a52e3f43c3f6abf63ed7259.html百度经验 在Excel中如何获取汉字拼音首字母?| 在工作中,为了方 ...
- Excel_提取某一列的汉字拼音首字母
适用需求_对于Excel表中,想要获取某一列的汉字拼音首字母 首先,点击alt+f11打开Microsoft Visual Basic for Applications(也就是offices中带的VB ...
最新文章
- go3--常量和类型转换
- 人本质要好,要善良,要真诚,有格局和胸怀,有能力,有眼光,能讲故事,能找到人,能搞到钱...
- python 爬虫实例-Python 爬虫:Scrapy 实例(二)
- velocity显示List与Map的方法详细解析
- ST-Link刷成J-Link
- Memcache架构新思考
- 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'Address'中的标识列指
- java多线程示例_Java线程示例
- 一文看尽2019年NLP前沿突破
- You specified a pre-MSA CPU in your CFLAGS
- pt100热电阻计算公式C语言,pt100计算公式,PT100实际应用中的两种形式
- 【爬虫】IP代理池的总结、实现与维护,IP代理池小工具(小框架),自建代理ip池
- wifi辐射安全距离
- yolov4中的mosaic数据增强
- 一元三次方程的求解(二分法)
- golang emoji表情处理
- 两种电致发光器件EQE测量方法(光分布法和积分球法)
- 【原创纯手打】如何用微信小程序写留言板(附代码)
- jar启动指定JDK/JRE 安装路径教程
- OKI Pro 9542 打印机驱动