统计中文字数
在 Vim
中可以容易地用 Ex 命令
:%s/[\u4e00-\u9fa5\u3040-\u30FF]//gn
统计。
统计英文单词数
在终端中可以使用 wc -w
统计。但如果英文中混有中文该怎么办呢?我写了这个 Python3 脚本 stripwchar.py
:
#!/usr/bin/env python3
import argparse
import unicodedata
import sys
def make_parser():
parser = argparse.ArgumentParser(
description=('This script strips wide characters from FILE or stdin '
'if one is not provided, and outputs to stdout.'))
parser.add_argument('filename', metavar='FILE', nargs='?')
return parser
def stripwchar(istream, ostream):
for line in istream:
cbuf = []
for c in line:
if unicodedata.east_asian_width(c) in 'FW'