闲着没事写了个小工具,将中文转拼音后填充到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. 从尾到头打印链表——剑指Offer

    https://www.nowcoder.net/practice/d0267f7f55b3412ba93bd35cfa8e8035?tpId=13&tqId=11156&tPage= ...

  2. C++ - 模板函数须要类型转换时使用友元(friend)模板函数

    模板函数须要类型转换时使用友元(friend)模板函数 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24357301 非模板函数 ...

  3. vim中自己主动加入凝视 加入文本信息

    工欲善其事,必先利其器.在开发过程中.方便.快捷的开发环境.能提高工作效率.优美的界面能让我们心情愉悦:最重要的是,能保持我们在外行严重高深莫測的牛逼~ 假设在创建新的源程序文件时希望能自己主动产生一 ...

  4. 【转】 vsftp上传文件出现553 Could not create file解决方法

    因工作需要,需要搭建一个ftp服务器,我使用ubuntu 10.04操作系统,下载vsftpdy源代码, 进行了编译,安装,然后按照INSTALL文件,创建了用户等操作.    因为时间比较紧,我采用 ...

  5. 西门子TCP/UDPport

    通过TCP和UDP数据传输的不同服务用到了哪些port? func=ll&objid=21874445&nodeid0=10806074&load=treecontent&am ...

  6. mybatis Generator生成代码及使用方式(转载)

    转载自:http://www.cnblogs.com/fengzheng/p/5889312.html 文章很棒,很不错,转了.

  7. volatile限定符

    volatile关键字是一种类型修饰符,用它声明的类型变量表示可以被某些编译器未知的因素更改,比如:操作系统.硬件或者其它线程等.由于访问寄存器的速度要快过RAM,所以编译器一般都会作减少存取外部RA ...

  8. poj2011

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17608   Accepted: 765 ...

  9. [POI 2018] Prawnicy

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5102 [算法] 首先,n条线段的交集一定是[Lmax,Rmin] , 其中,Lmax ...

  10. 特征选择--->卡方选择器

    特征选择(Feature Selection)指的是在特征向量中选择出那些“优秀”的特征,组成新的.更“精简”的特征向量的过程.它在高维数据分析中十分常用,可以剔除掉“冗余”和“无关”的特征,提升学习 ...