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,不过包含后包肯定是会变大的,于是....自己写了一个硬枚举的中文转拼音的类. 原理是这样的 ...
随机推荐
- Ubuntu 16.04设置文件夹试图永久以列表显示
图片来自:http://forum.ubuntu.org.cn/viewtopic.php?p=2043385
- MySQL大小写问题的简单说明(关键字/函数/表名)(转)
MySQL语句中字母大小写规则随着语句元素的不同而变化,同时还要取决于MySQL服务器主机上的操作系统. SQL关键字与函数名 关键字和函数名不区分字母的大小写.如.abs.bin.now.versi ...
- [转]SQL Server编程:SMO介绍
转自:周公 最近在项目中用到了有关SQL Server管理任务方面的编程实现,有了一些自己的心得体会,想在此跟大家分享一下,在工作中用到了SMO/SQL CLR/SSIS等方面的知识,在国内这方面的文 ...
- 深入学习理解java-ThreadLocal
导读 首先,ThreadLocal 不是用来解决共享对象的多线程訪问问题的,普通情况下,通过ThreadLocal.set() 到线程中的对象是该线程自己使用的对象,其它线程是不须要訪问的,也訪问不到 ...
- 鸟哥的Linux私房菜-----10、学习Bash
- ios2--UIView的常见属性
// // ViewController.m // 06-UIView的常见属性 // #import "ViewController.h" @interface ViewCont ...
- java事件处理机制(自定义事件)
java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中,作为参数,一般存在与listerner的方法之中 2.event sourc ...
- vs打开wixproj后缀文件
1.在正常情况下vs是无法打开wixproj工程的,能打开也只能是以记事本方式打开该文件本身 2.所以此时需要下载wixtool,安装后即可打开上述类型文件 3.最好也安装好vs对应版本的扩展包 4. ...
- PCB NOSQL MongoDb MI流程指示数据存储结构
一.MI流程指示结构 二.产品型号树结构(即盲埋孔板型号结构) 三.MI流程指示UI 小结:1.MI流程指示使用的表非常之多(30多张表),存储的数据分散到各个表中,而NOSQL 一个产品型号一条记录 ...
- Potted Flower(线段树+dp)
http://poj.org/problem?id=2750 题意:在一个圈中取若干个相邻的数,求他们的最大序列和.不能够同时取所有的数. 看了一篇解题报告写的很详细..http://blog.csd ...