使用Python编写可以从文件读取的程序,需要输出第1行后的字符数和字符的重复

时间:2012-03-24 08:56:00

标签: python

这是我第一次使用Python,而且我对编程的总体经验很少。我必须使用Python构建一个可以从文件中读取的程序,需要输出第1行后的字符数和字符的重复次数。我很挣扎,不知道如何计算文件的单个字符。我必须在文件中找到“C”和“G”的重复,但我没有任何线索。 这就是我所做的:

while True:
    try:    
        filename = raw_input('Enter file:')

        filename = open(filename, "r+")

        line1 = filename.readline()
        num_lines = 0
        num_words = 0
        num_chars = 0
        len_line1 = len  (line1)

        for line in filename:
            words = line.split()

            num_lines += 1
            num_words += len(words)
            num_chars += len(line)

    except:
        break

print "Output is : ", line1

print "Length : ", num_chars - len_line1

print filename.split().index('C')

2 个答案:

答案 0 :(得分:1)

你已经拥有了大部分需要的东西。干得好!

为了计算cg的出现次数,请记住可以迭代一条线。
假设我们有几个变量num_cnum_g 然后,当我们遇到这两个字母中的一个时,我们可以循环遍历这些变量的每一行。

以下是尝试,但与您的尝试并不完全相同 所以修改你的。祝你好运!

with open('/path/to/the/file.txt','r') as fin:
    num_chars = 0
    num_lines = 0
    num_words = 0
    num_c = 0
    num_g = 0
    fin.next()
    for line in fin:
        num_lines += 1
        num_words += len(line.split())
        for char in line:
            num_chars += 1
            if char.lower() == 'c':
                num_c += 1
            elif char.lower() == 'g':
                num_g += 1

答案 1 :(得分:1)

如果您计划对此进行扩展,您可能会发现将计数存储在数据结构中更容易,因此您不必继续添加if语句的行和行,检查每个字符以查找您感兴趣的字符

以bernie的例子为基础,

from collections import defaultdict

with open('/path/to/the/file.txt','r') as fin:
    num_chars= 0
    character_dict= defaultdict(int)
    fin.next()
    for line in fin:
        num_chars += len(line)
        for char in line:
            character_dict[char] += 1
            # if you dont want to count upper and lower case letters seperately then use the following line instead
            #character_dict[char.upper()] += 1

print "There are %i 'G's" %character_dict["G"]
print "There are %i 'C's" %character_dict["C"]