吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 845|回复: 4
收起左侧

来个大表哥帮忙写个python脚本

[复制链接]
可爱先生 发表于 2020-3-31 09:43
40吾爱币
要求把txt文本里面的词语转换成拼音 例如:合作 转成 hezuo
然后一行一个 单独输出到另一个txt文本
来个表哥帮帮忙
词语.png

最佳答案

查看完整内容

先安装 pypinyin pip install pypinyin [mw_shl_code=python,true] from pathlib import Path from pypinyin import lazy_pinyin def hanzi2pinyin(string): py = lazy_pinyin(string) #如果不要空格删除这里的空格 return ' '.join(py) def readfile(file): if Path(file).exists(): with open(file, "r", encoding='utf-8') as f: # 打开文件 data = f.read() # 读取文件 ...

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

pzx521521 发表于 2020-3-31 09:43
本帖最后由 pzx521521 于 2020-3-31 10:26 编辑

先安装 pypinyin
pip install pypinyin
[Python] 纯文本查看 复制代码
from pathlib import Path
from pypinyin import lazy_pinyin

def hanzi2pinyin(string):
    py = lazy_pinyin(string)
    #如果不要空格删除这里的空格
    return ' '.join(py)
def readfile(file):
    if Path(file).exists():
        with open(file, "r", encoding='utf-8') as f:  # 打开文件
            data = f.read()  # 读取文件
            list = data.split("\n")
            return list

def writefile(file, list):
    content = ''
    for i in range(len(list)):
        py = hanzi2pinyin(list[i])
        content = content + py + '\n'
    with open(file, 'w') as fw:
        fw.write(content)

if __name__ == '__main__':
    list = readfile("a.txt")
    writefile("b.txt", list)

免费评分

参与人数 2吾爱币 +1 热心值 +2 收起 理由
hzmilk + 1 热心回复!
tzy926 + 1 + 1 用心讨论,共获提升!

查看全部评分

头像被屏蔽
tzy926 发表于 2020-3-31 09:45
 楼主| 可爱先生 发表于 2020-3-31 10:08
头像被屏蔽
tzy926 发表于 2020-3-31 10:36
提示: 作者被禁止或删除 内容自动屏蔽
快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-6 09:07

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表