python 将中文转拼音后填充到url做参数并写入excel
闲着没事写了个小工具,将中文转拼音后填充到url做参数并写如excel
一.先看下演示,是个什么东西


二.代码
代码用到一个中文转拼音的库,库是网上下的,稍微做了下修改,已经找不原来下载的地址了,然后需要装个pywin32库,用来写excel表格的,下面看代码.
#!/usr/bin/env python
# coding=utf-8
# Author: ca0gu0 from lib.chinese2pinyin import search
from time import sleep
import win32com.client as win32
import getopt, sys class Excel(object):
def __init__(self):
app = 'Excel'
xl = win32.gencache.EnsureDispatch('%s.Application' %app)
ss = xl.Workbooks.Add()
self.sh = ss.ActiveSheet
xl.Visible = True def write(self, row, column, string):
sleep(0.1)
print string, row, column
try:
self.sh.Cells(row,column).Value = u'%s' %string
except UnicodeDecodeError:
self.sh.Cells(row,column).Value = '%s' %string def Links(url,title,readfile):
fobj = open(readfile, 'r')
excel = Excel()
title = title.split(',')
for column,string in enumerate(title):
column += 1
excel.write(1,column, string) row = 2
for eachline in fobj:
LIST = eachline.split()
PIN = []
for ch in LIST:
#ch = ch.decode('utf-8')
#ch = ch.encode('gbk')
result = search(ch)
PIN.append(result)
try:
tp = tuple(PIN)
link = url %tp
LIST.append(link)
print LIST for column,string in enumerate(LIST):
column += 1
excel.write(row,column, string)
except TypeError,e:
print e
row += 1
fobj.close() def usage():
print u"Usage: python trf_excel.py -u http://www.xxcc.cn/?%s?%s?%s -t 计划,单元,关键词,链接" def main():
if len(sys.argv) <2:
usage()
try:
opts, args = getopt.getopt(sys.argv[1:], "hu:t:", ["url=", "title="])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
for o, a in opts:
if o in ("-h"):
usage()
if o in ("-u", "--url"):
url = a
print url
if o in ("-t", "--title"):
title = a
print title readfile = 'citiao.txt'
Links(url,title,readfile) if __name__ == '__main__':
main()
可以命令行执行: python trf_excel.py -u http://www.xxcc.cn/?%s?%s?%s -t 字段1,字段2,字段3,链接
注意:url地址占位符用%s, -t 这个是生成excel字段标题,然后要转成中文的放到citiao.txt文件中
三.下载地址
已经放到github上了
git clone git@github.com:ca0gu0/tools.git
python 将中文转拼音后填充到url做参数并写入excel的更多相关文章
- Python 将中文转拼音
文字转拼音 import os.path class PinYin(object): def __init__(self): self.word_dict = {} def load_word(sel ...
- vue路由\导航刷新后:ative\localStorage\url截取参数
<el-menu :default-active="$route.path" router mode="horizontal"> <el-me ...
- Python中文转拼音代码(支持全拼和首字母缩写)
本文的代码,从https://github.com/cleverdeng/pinyin.py升级得来,针对原文的代码,做了以下升级: 1 2 3 4 1.可以传入参数firstcode:如果为 ...
- [Python] Python 获取中文的首字母 和 全部拼音首字母
Python 获取中文的首字母 和 全部拼音首字母 代码如下: import pinyin def getStrAllAplha(str): return pinyin.get_initial(str ...
- python中文utf8编码后是占3个字符,unicode汉字为2字节
一个中文utf8编码后是占3个字符,所以求长度的函数可以这样写 def str_len(str): try: row_l=len(str) utf8_l=len(str.encode('utf-8') ...
- Awesome Python(中文对照)
python中文资源大全:https://github.com/jobbole/awesome-python-cn A curated list of awesome Python framework ...
- SQL 用中文的拼音和笔画排序
SQL 用中文的拼音和笔画排序 城市按拼音排序: SELECT chineseName FROM [表名] order by chinesename collate Chinese_PRC_CS_ ...
- python读取中文文件编码问题
python 读取中文文件后,作为参数使用,经常会遇到乱码或者报错asii错误等. 我们需要对中文进行decode('gbk') 如我有一个data.txt文件有如下内容: 百度 谷歌 现在想读取文件 ...
- 中文转拼音without CJK
Xamarin写Android程序时,通常要使用按中文首字母分组显示(如通讯录) . 于是需要被迫包含CJK,不过包含后包肯定是会变大的,于是....自己写了一个硬枚举的中文转拼音的类. 原理是这样的 ...
随机推荐
- gh-ost: triggerless online schema migrations:Blog by Shlomi Noach:
http://code.openark.org/blog/category/mysql https://rj03hou.github.io/mysql/gh-ost/
- 未来 Web 设计的 7 大趋势
1.手势代替点击 还记得曾经是怎样滚动网页的吗?将鼠标移到屏幕的右边缘,然后拖动古代称为"滚动栏"的玩意儿: 略微专业点的可能会使用鼠标滚轮,光标键或触控板,这已经率先于大多数的用 ...
- redis 主从备份自动切换+java代码实现类
转载:http://blog.csdn.net/qq_23430789/article/details/52185706 目录(?)[-] redis-0sentinel实例之间的通讯端口 maste ...
- spark sql读hbase
项目背景 spark sql读hbase据说官网如今在写,但还没稳定,所以我基于hbase-rdd这个项目进行了一个封装,当中会区分是否为2进制,假设是就在配置文件里指定为#b,如long#b,还实用 ...
- AutoCAD如何移动坐标原点
通常在CAD画图设计时,坐标原点都默认在左下角,下面就来分享一下在CAD如何把左下角的坐标原点移动到我们画的图形中心点: 1.输入坐标原点移动命令UCS: 按回车确认后,再输入M(就是移动的意思): ...
- 1695 windows 2013
1695 windows 2013 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 话说adam ...
- 对数据html文本 的处理
对数据html文本 的处理 : 提取文字.图片.分句 ''' SELECT * FROM Info_Roles WHERE Flag=1 LIMIT 2; select top y * from 表 ...
- There was a conflict between
解读,首先搜索到第一个5>的开头的那一行,确认是在编译哪一个项目. 那么后面的冲突,就是在和这个项目冲突. There was a conflict between "log4net, ...
- 浅谈JVM内存模型
JAVA虚拟机在执行JAVA程序的时候,会把它管理的内存分成若干不同的数据区域,每个区域都有各自的用途.目前大致把JVM内存模型划分为五个区域:程序计数器,虚拟机栈,本地方法栈,堆和方法区. 程序计数 ...
- createrepo
[root@iio enp]# createrepo -g /enp/comps.xml .Spawning worker 0 with 1362 pkgsWorkers FinishedSaving ...