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

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理
以下文章来源于小蚊子数据分析, 作者小蚊子数据分析
如何统计文本中的中英文字符数?Python帮你解决文章插图
代码 1import string 2import pandas as pd 3import csv 4 5 6blocks = [] 7def str_count(str): 8'''找出字符串中的中英文、空格、数字、标点符号个数''' 9count_en = count_dg = count_sp = count_zh = \10count_pu = count_at = count_tan = count_wen = count_mao = count_jing = count_sheng = 01112for s in str:13# 英文14if s in string.ascii_letters:15count_en += 116# 数字17elif s.isdigit():18count_dg += 119# 空格20elif s.isspace():21count_sp += 122# 中文23elif s.isalpha():24count_zh += 125# 特殊字符26else:27for ss in s[:]:28if ss == '@':29count_at += 130if ss == '!':31count_tan += 132if ss == '?':33count_wen += 134if ss == ':':35count_mao += 136if ss == '#':37count_jing += 138if '......' in s:39count_sheng += 140count_pu += 141print('英文字符:', count_en)42print('数字:', count_dg)43print('空格:', count_sp)44print('中文:', count_zh)45print('特殊字符:', count_pu)46print('@:', count_at)47print('!:', count_tan)48print('?:', count_wen)49print(':', count_mao)50print('#:', count_jing)51print('......', count_sheng)52block = [count_en, count_dg, count_sp, count_zh, count_pu, count_at, count_tan, count_wen, \53count_sheng, count_mao, count_jing]54blocks.append(block)555657f = pd.read_csv('C://Users//lecce//Desktop//1015.csv')58for i in range(0, 569):59str_count(f.content[i])6061with open('content.csv', 'w', newline='') as csvfile:62writer = csv.writer(csvfile)63for x in range(0, len(blocks)):64writer.writerow(blocks[x])【如何统计文本中的中英文字符数?Python帮你解决】如何统计文本中的中英文字符数 , 在Excel中用LEN函数无果后 。 只好拿出python , 从网上开始抄代码 , 经过一系列复制粘贴和拼凑 , 最终达到可用的状态 。 可能还有些许bug 。


    推荐阅读