• 中文转拼音的方法


    pip install pypinyin

    import pypinyin
    data=u'中文转拼音的方法'
    py = pypinyin.pinyin(data, style=pypinyin.NORMAL)
    ret = ''.join([i[0] for i in py])
    print(ret)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    写成服务的话,大概是这样:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    __author__ = 'hello@yeshen.org'
    
    
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    
    from bottle import route, run, template, run, post, request, response, get, static_file
    import sys,argparse,pypinyin
    
    
    @route('/pinyin')
    def html_rtc():
        data = ''
        with open('pinyin.html', 'r') as f:
            data = f.read()
        return data
    
    
    @post('/pinyin')
    def post_text():
        data = request.json['content']
        py = pypinyin.pinyin(data, style=pypinyin.NORMAL)
        ret = ''.join([i[0] for i in py])
        print ret
        return ret
        
    
    def server():
        run(host='127.0.0.1', port=2022)
    
    
    def local():
        arg_parser = argparse.ArgumentParser(description='chinese to pinyin')
        arg_parser.add_argument(
            '-f',
            '--file',
            help='file path, [test.txt] for default',
            action='store',
            default='t.txt')
        
        args = arg_parser.parse_args(sys.argv[1:])
        print("file: %s" % args.file)
        ret = ''
    
        with open(args.file) as f:
            for line in f:
                py = pypinyin.pinyin(unicode(line.strip(), "utf-8"), style=pypinyin.NORMAL)
                ret += ''.join([i[0] for i in py]) + '\n'
        
        print(ret)
    
    
    
    if __name__ == '__main__':
        server()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="author" content="hello@yeshen.org">
    <meta name="description" content="what makes people drawn to each other and stay in those relationships?">
    <title>pin yintitle>
    <style type="text/css">
    body{margin: 0px;background-color: #f1f1f1;}
    #text-content{ display: flex; }
    #current {
        flex: auto;
        text-align: left;
        box-shadow: none;
        border: none;
        box-shadow: none;
        -webkit-box-shadow: none;
        outline:none;
        font-size: 16px;
    }
    #last {
      padding: 8px;
      color: #999;
      font-size: 16px;
    }
    style>
    <script>
    var file_url = "/pinyin"
    
    function ontextchange(){
      var x=document.getElementById("current");
      fetch(file_url, {
          method: 'POST',
          body: JSON.stringify({ "content" : x.value}),
          headers: {'Content-Type': 'application/json'}
      }).then(res => res.text()).then(res => {
          document.getElementById("last").innerText = res;
          console.log(res);
      });
    }
    
    script>
    head>
    <body>
      <div id="text-content">
        <textarea id="current" rows="15" onchange="ontextchange()">textarea>
      div>
      <div id="last">div>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    视频号视频怎么保存到手机,视频号视频怎么保存到手机相册里,苹果手机电脑都可以用
    【Python】网络编程
    51单片机学习:ADC模数转换实验--光敏电阻AD采集
    .net core-利用BsonDocumentProjectionDefinition和Lookup进行 join 关联查询(MongoDB)
    Type-challehges learning: pick
    华为USG6000V防火墙v1
    《无限可能-快速唤醒你的学习脑》阅读笔记
    一文看懂Mysql锁
    添加一个仅管理员可见的页面
    centralwidget 不能布局
  • 原文地址:https://blog.csdn.net/yeshennet/article/details/127591605