002
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
"""统计文本文件的中文字符数、总字符数、非空白字符数。"""
|
||||
import argparse
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def stats(text: str) -> dict:
|
||||
return {
|
||||
"chars": len(text),
|
||||
"nonspace": len(re.sub(r"\s", "", text)),
|
||||
"zh": len(re.findall(r"[\u4e00-\u9fff]", text)),
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Count Chinese characters in text files.")
|
||||
parser.add_argument("paths", nargs="+", help="Files to count.")
|
||||
args = parser.parse_args()
|
||||
|
||||
for path_str in args.paths:
|
||||
path = Path(path_str)
|
||||
text = path.read_text(encoding="utf-8")
|
||||
s = stats(text)
|
||||
print(f"{path}: 中文字符={s['zh']}, 总字符={s['chars']}, 非空白字符={s['nonspace']}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user