我是商科的,基础python项目。

img

img

img

img

Write a Python program to count the frequency of words in a file.

wx可以私信我

你好,已完成图片上述功能,已私信

class Time:
    def __init__(self, hour, minute, second):
        self.hour = hour
        self.minute = minute
        self.second = second

    def print_hour(self):
        print('{hour}:{minute}:{second}'.format(hour=self.hour, minute=self.minute, second=self.second))


class BankAccount:
    def __init__(self, name='Smith', balance=1000):
        self.name = name
        self.balance = balance

    def deposit(self, _sum):
        self.balance += _sum

    def withdraw(self, _sum):
        self.balance -= _sum

    def display(self):
        print('The balance of the bank account of {name} is {balance} euros.'.format(name=self.name,
                                                                                     balance=self.balance))


class Orange:
    def __init__(self, age, height, oranges):
        self.age = age
        self.height = height
        self.oranges = oranges

    def isMature(self):
        if self.age < 5:
            return 'The tree is {age} years old, too young to process any fruit'.format(age=self.age)
        elif 15 > self.age >= 5:
            return 'The tree is {age} years old and produced {oranges} fruits'.format(age=self.age,
                                                                                      oranges=self.oranges)
        elif 70 > self.age >= 15:
            return 'The tree is {age} years old and produced {oranges} fruits'.format(age=self.age,
                                                                                      oranges=self.oranges)
        else:
            return 'The tree is {age} years old and don\'t produce fruits any longer'.format(age=self.age)

    def isAlive(self):
        if self.age <= 50:
            return 'It\'s alive !'
        elif 100 >= self.age > 50:
            return 'It\'s dying increases each year.'
        else:
            return 'The tree is dead :('

    def pickAnOrange(self):
        self.oranges -= 1
        return 'I just picked an orange from the tree, it\'s delicious !'

    def passGrowingSeason(self):
        if self.height < 25:
            self.height += 1
            return 'It grows and it\'s height now is {height}m'.format(height=self.height)
        return 'It\'s height is 25m and no grow'


def read_file_and_remove(input_file_name, output_file_name):
    with open(input_file_name, "r") as fr:
        lines = fr.readlines()

    with open(output_file_name, "w") as fw:
        for line in lines:
            if line[0] != '#':
                fw.write(line)
    print(f'New file is {file_name}.'.format(file_name=output_file_name))


def read_file_longest_long(input_file_name):
    with open(input_file_name, "r") as fr:
        lines = fr.readlines()

    longest_length = 0
    longest_line = None
    for line in lines:
        line_len = len(line)
        if line_len > longest_length:
            longest_length = line_len
            longest_line = line
    print('The longest line is: \n{longest_line}\n'.format(longest_line=longest_line))
    print('The length of the longest lines is {longest_length}'.format(longest_length=longest_length))
    return longest_line, longest_length


def replace_word(input_file_name):
    with open(input_file_name, "r") as fr:
        content = fr.read()

    return content.replace(' ', '***')


def read_frequency(input_file_name):
    with open(input_file_name, "r") as fr:
        lines = fr.readlines()

    frequency_dict = {}
    for line in lines:
        words = line.replace('\n', '').replace(',', '').replace('.', '').split(' ')
        for word in words:
            if word not in frequency_dict.keys():
                frequency_dict[word] = 0
            frequency_dict[word] += 1
    print('frequency_dict', frequency_dict)

    for word, number in frequency_dict.items():
        print('The frequency of the word \'{word}\' is: {number}.'.format(word=word, number=number))
    return


print("##########################################################")
print("EXERCICE 1 ")
print("##########################################################")
one_time = Time(10, 15, 18)
one_time.print_hour()

print("##########################################################")
print("EXERCICE 2 ")
print("##########################################################")
account1 = BankAccount('Duchmol', 800)
account1.deposit(350)
account1.withdraw(200)
account1.display()


print("##########################################################")
print("EXERCICE 3 ")
print("##########################################################")
one_orange_tree = Orange(10, 10, 100)
print(one_orange_tree.isMature())
print(one_orange_tree.isAlive())
print(one_orange_tree.pickAnOrange())
print(one_orange_tree.passGrowingSeason())

print("##########################################################")
print("EXERCICE 5 ")
print("##########################################################")
file_name = 'input.txt'
new_file_name = 'output.txt'
read_file_and_remove(file_name, new_file_name)

print("##########################################################")
print("EXERCICE 6 ")
print("##########################################################")
file_name = 'input.txt'
print(read_file_longest_long(file_name))

print("##########################################################")
print("EXERCICE 7 ")
print("##########################################################")
file_name = 'input.txt'
print(replace_word(file_name))

print("##########################################################")
print("EXERCICE 8 ")
print("##########################################################")
file_name = 'input.txt'
read_frequency(file_name)