【问题标题】:Python character and word countsPython 字符和字数
【发布时间】:2015-06-03 00:25:15
【问题描述】:

我是 python 的初学者,想知道如何使用两个 txt 文件来计算字符以及计算 10 个最常见的字符。以及如何将文件中的所有字符转换为小写并消除除a-z之外的所有字符

这是我尝试过但没有成功的方法:

from string import ascii_lowercase
from collections import Counter

with open ('document1.txt' , 'document2.txt') as f:
    print Counter(letter for line in f
                    for letter in line.lower()
                    if letter in ascii_lowercase)

【问题讨论】:

  • 您收到的错误是什么,而且您的 with 语句格式不正确。使用 open("file.txt", "r") 作为数据:不能使用相同的 with 语句打开两个文件。你需要两个 with 语句。

标签: python string count character counter


【解决方案1】:

试试这样:

>>> from collections import Counter
>>> import re
>>> words = re.findall(r'\w+', "{} {}".format(open('your_file1').read().lower(), open('your_file2').read().lower()))
>>> Counter(words).most_common(10)

【讨论】:

    【解决方案2】:

    这是一个简单的例子。您可以调整此代码以满足您的需求

    from string import ascii_lowercase
    from collections import Counter
    
    with open('file1.txt', 'r') as file1data: #opening an reading file one
        file1 = file1data.read().lower() #convert the entire file contents to lower
    
    with open('file2.txt', 'r') as file2data: #opening an reading file two
        file2 = file2data.read().lower() 
    
    #The contents of both file 1 and 2 are stored in fil1 and file2 variables
    #Examples of how to work with one file repeat for two files
    file1_list = []
    for ch in file1:
        if ch in ascii_lowercase: #makes sure only lowercase alphabet is appended.  All Non alphabet characters are removed
            file1_list.append(ch)
        elif ch in [" ", ".", ",", "'"]: #remove this elif block is you just want the letters
            file1_list.append(ch) #make sure basic punctionation is kept
    
    print "".join(file1_list) #this line is not needed. Just to show what the text looks like now
    print Counter(file1_list).most_common(10) #prints the top ten
    print Counter(file1_list) #prints the number of characters and how many times they repeat
    

    现在您已经查看了上面的混乱并了解了每一行的作用,这里有一个更简洁的版本,可以满足您的需求。

    from string import ascii_lowercase
    from collections import Counter
    
    with open('file1.txt', 'r') as file1data: 
        file1 = file1data.read().lower()
    
    with open('file2.txt', 'r') as file2data: 
        file2 = file2data.read().lower() 
    
    file1_list = []
    for ch in file1:
        if ch in ascii_lowercase: 
            file1_list.append(ch)
    
    file2_list = []
    for ch in file2:
        if ch in ascii_lowercase: 
            file2_list.append(ch)
    
    
    
    all_counter = Counter(file1_list + file2_list) 
    top_ten_counter = Counter(file1_list + file2_list).most_common(10) 
    
    print sorted(all_counter.items()) 
    print sorted(top_ten_counter)
    

    【讨论】:

    • 谢谢。这确实有效,但它正在拉起整个文件。我如何让它只显示柜台?还有我如何让它显示为:a 20 b 10 c 14 etc 而不是 20 b 10 c 14
    • 对上面的代码进行了修改,应该可以为您解决问题。
    【解决方案3】:

    不幸的是,没有办法在不重写文件的情况下插入文件的中间。正如之前的海报所指出的那样,您可以使用 seek 附加到文件或覆盖文件的一部分,但如果您想在开头或中间添加内容,则必须重写它。

    这是操作系统的事情,而不是 Python 的事情。在所有语言中都是一样的。

    我通常做的是从文件中读取,进行修改并将其写入一个名为 myfile.txt.tmp 或类似文件的新文件。这比将整个文件读入内存要好,因为文件可能太大了。临时文件完成后,我将其重命名为与原始文件相同。

    这是一种很好、安全的方法,因为如果文件写入因任何原因崩溃或中止,您仍然拥有未触及的原始文件。

    要从多个文件中找到最常见的words

    from collections import Counter
    import re
    with open(''document1.txt'') as f1, open(''document1.txt'') as f2:
        words = re.findall(r'\w+', f1.read().lower()) + re.findall(r'\w+', f2.read().lower())
        >>>Counter(words).most_common(10)
        "wil give you most 10 common words"
    

    如果你想要最常见的 10 个characters

    >>>Counter(f1.read() + f2.read()).most_common(10)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 2021-01-22
      • 1970-01-01
      • 2021-12-11
      • 2016-06-24
      • 2020-12-02
      • 2016-02-09
      相关资源
      最近更新 更多