在python3.x中分析文本文件中的某些属性

2024-06-01 08:01:41 发布

您现在位置:Python中文网/ 问答频道 /正文

对于python3.x中的赋值,我必须创建一个程序来读取文本文件并输出字符、行、元音、大写字母、数字和单词的总数。用户必须提供文本文件的文件和路径。要文件很容易:

file = input("Please provide the file path and file name. \nFor example C:\\Users\\YourName\\Documents\\books\\book.txt \n:")
f = open(file, 'r')
text = f.read()

我试着使用一些简单的函数,比如:

^{pr2}$

…但进一步阅读赋值会发现,我必须使用for循环来分析字符串中的每个字符,然后使用多方向if语句来检查它是否是元音、数字等

我知道我可以通过计算\n的数目来计算行数,我也可以为wordsl使用.split()函数,但我对如何去做相当茫然。在

我想把输出格式化成这样,虽然我想我可以在程序运行后解决这个问题。在

------------width=35---------|--width=8----
|number of characters        :       #####|
|number of lines             :       #####|
|number of vowels            :       #####|
|number of capital letters   :       #####|
|number of numeric digits    :       #####|
|number of words             :       #####|

如果有人能帮我上路并告诉我该怎么做,我将不胜感激。在


Tags: 文件of函数程序number数字大写字母字符