闲着没事写了个小工具,将中文转拼音后填充到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的更多相关文章

  1. Python 将中文转拼音

    文字转拼音 import os.path class PinYin(object): def __init__(self): self.word_dict = {} def load_word(sel ...

  2. vue路由\导航刷新后:ative\localStorage\url截取参数

    <el-menu :default-active="$route.path" router mode="horizontal"> <el-me ...

  3. Python中文转拼音代码(支持全拼和首字母缩写)

    本文的代码,从https://github.com/cleverdeng/pinyin.py升级得来,针对原文的代码,做了以下升级:     1 2 3 4 1.可以传入参数firstcode:如果为 ...

  4. [Python] Python 获取中文的首字母 和 全部拼音首字母

    Python 获取中文的首字母 和 全部拼音首字母 代码如下: import pinyin def getStrAllAplha(str): return pinyin.get_initial(str ...

  5. python中文utf8编码后是占3个字符,unicode汉字为2字节

    一个中文utf8编码后是占3个字符,所以求长度的函数可以这样写 def str_len(str): try: row_l=len(str) utf8_l=len(str.encode('utf-8') ...

  6. Awesome Python(中文对照)

    python中文资源大全:https://github.com/jobbole/awesome-python-cn A curated list of awesome Python framework ...

  7. SQL 用中文的拼音和笔画排序

    SQL 用中文的拼音和笔画排序   城市按拼音排序: SELECT chineseName FROM [表名] order by chinesename collate Chinese_PRC_CS_ ...

  8. python读取中文文件编码问题

    python 读取中文文件后,作为参数使用,经常会遇到乱码或者报错asii错误等. 我们需要对中文进行decode('gbk') 如我有一个data.txt文件有如下内容: 百度 谷歌 现在想读取文件 ...

  9. 中文转拼音without CJK

    Xamarin写Android程序时,通常要使用按中文首字母分组显示(如通讯录) . 于是需要被迫包含CJK,不过包含后包肯定是会变大的,于是....自己写了一个硬枚举的中文转拼音的类. 原理是这样的 ...

随机推荐

  1. RESTFUL 和SOA初探

    这篇文章是转载的,restful简单的说就是url明确的指向资源.soa还不好用自己的话解释,但明显不是这样,好吧,我自己的理解就是soa就是访问网站的一个接口.以访问一个blog list为例子,  ...

  2. 【CV论文阅读】Network in Network

    目的: 通过用Mlpconv层来替代传统的conv层,可以学习到更加抽象的特征.传统卷积层通过将前一层进行了线性组合,然后经过非线性激活得到(GLM),作者认为传统卷积层的假设是基于特征的线性可分.而 ...

  3. webservice Connection timed out

    webservice Connection timed out,当发生webservice的链接超时错误时.我想原因无非就是webclient到webservice之间的链接通路发生了异常,那么该怎样 ...

  4. GDIPlus绘制桌面歌词

    功能介绍 採用GDIPlus绘制桌面歌词,相似酷狗.QQ音乐等软件.歌词支持纯色.两色渐变.三色渐变:支持高亮歌词. 实现方法 窗体部分:桌面歌词是个独立的背景透明窗体.能够移动位置,能够鼠标穿透.透 ...

  5. LeetCode 824. Goat Latin (山羊拉丁文)

    题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...

  6. unity3D游戏开发实战原创视频讲座系列11之相扑游戏开发并公布到Win\WP8

     解说文件夹 第一讲 游戏的演示和资源介绍 第二讲 场景的建设 第三讲 玩家的移动 第四讲 对手的AI(让对手动起来) 第五讲 游戏的管理(上) 第六讲 游戏的管理(下) 第七讲 公布到Win8系 ...

  7. Xshell配色方案啊【学习笔记】

    自己移植从putty版本移植到Xshell的配色方案,效果不错,看上去挺舒服. [myisayme] text(bold)=eaeaea magenta(bold)=ff55ff text=fffff ...

  8. 深度学习的seq2seq模型——本质是LSTM,训练过程是使得所有样本的p(y1,...,yT‘|x1,...,xT)概率之和最大

    from:https://baijiahao.baidu.com/s?id=1584177164196579663&wfr=spider&for=pc seq2seq模型是以编码(En ...

  9. [luoguP4142]洞穴遇险

    https://www.zybuluo.com/ysner/note/1240792 题面 戳我 解析 这种用来拼接的奇形怪状的东西,要不就是轮廓线\(DP\),要不就是网络流. 为了表示奇数点(即\ ...

  10. Another lottery

    http://acm.hdu.edu.cn/showproblem.php?pid=2985 题意:有n个人每个人可以买m轮彩票,每轮可以买尽可能多的彩票.如果该彩票在i轮被抽到,则该人可以获得2^i ...