zoukankan      html  css  js  c++  java
  • 如何统计文本中的中英文字符数?Python帮你解决

    本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理

    以下文章来源于小蚊子数据分析 ,作者小蚊子数据分析

    如何统计文本中的中英文字符数?Python帮你解决

     

    代码

     1import string
     2import pandas as pd
     3import csv
     4
     5
     6blocks []
     7def str_count(str):
     8    '''找出字符串中的中英文、空格、数字、标点符号个数'''
     9    count_en count_dg count_sp count_zh 
    10        count_pu count_at count_tan count_wen count_mao count_jing count_sheng 0
    11
    12    for in str:
    13        # 英文
    14        if in string.ascii_letters:
    15            count_en += 1
    16        # 数字
    17        elif s.isdigit():
    18            count_dg += 1
    19        # 空格
    20        elif s.isspace():
    21            count_sp += 1
    22        # 中文
    23        elif s.isalpha():
    24            count_zh += 1
    25        # 特殊字符
    26        else:
    27            for ss in s[:]:
    28                if ss == '@':
    29                    count_at += 1
    30                if ss == '!':
    31                    count_tan += 1
    32                if ss == '?':
    33                    count_wen += 1
    34                if ss == ':':
    35                    count_mao += 1
    36                if ss == '#':
    37                    count_jing += 1
    38            if '......' in s:
    39                count_sheng += 1
    40            count_pu += 1
    41    print('英文字符:', count_en)
    42    print('数字:', count_dg)
    43    print('空格:', count_sp)
    44    print('中文:', count_zh)
    45    print('特殊字符:', count_pu)
    46    print('@:', count_at)
    47    print('!:', count_tan)
    48    print('?:', count_wen)
    49    print(':', count_mao)
    50    print('#:', count_jing)
    51    print('......', count_sheng)
    52    block [count_en, count_dg, count_sp, count_zh, count_pu, count_at, count_tan, count_wen, 
    53                                                           count_sheng, count_mao, count_jing]
    54    blocks.append(block)
    55
    56
    57f pd.read_csv('C://Users//lecce//Desktop//1015.csv')
    58for in range(0, 569):
    59    str_count(f.content[i])
    60
    61with open('content.csv', 'w'newline='') as csvfile:
    62    writer csv.writer(csvfile)
    63    for in range(0, len(blocks)):
    64        writer.writerow(blocks[x])
    

    如何统计文本中的中英文字符数,在Excel中用LEN函数无果后。只好拿出python,从网上开始抄代码,经过一系列复制粘贴和拼凑,最终达到可用的状态。可能还有些许bug。

  • 相关阅读:
    通过user-agent判断h5页面是在哪个手机App(QQ、微信、支付宝)下打开的
    vscode格式化插件
    简单直接,“NODE_ENV”总结
    NativeScript又一个Hybrid技术(附与Weex,ReactNative比较)
    ReactNative开发中遇到的问题记录
    两个在线编辑网站runjs和jsbin
    go 如何单测
    go语法-type等
    go语法-结构体和接口-细节
    解决 Webstorm 每次更新 Git 代码都要输入密码的问题
  • 原文地址:https://www.cnblogs.com/hhh188764/p/13826167.html
Copyright © 2011-2022 走看看