许文德

导航

实验五

task1.1
with open('data1_1.txt', 'r', encoding = 'utf-8') as f: data = f.readlines() n = 0 for line in data: if line.strip('\n') == '': continue n += 1 print(f'共{n}行')

 

 

 task1.2

with open('data1_1.txt', 'r', encoding = 'utf-8') as f:
    n = 0
    for line in f:
        if line.strip('\n') == '':
            continue
        n += 1

print(f'共{n}行')

task1.3

with open('data1_2.txt', 'r', encoding = 'utf-8') as f:
    n = 0
    for line in f:
        if line.strip() == '':
            continue
        n += 1

print(f'共{n}行')

task1.4

with open('data1_2.txt', 'r', encoding = 'utf-8') as f:
    n = 0
    for line in f:
        if line.isspace():
            continue
        n += 1

print(f'共{n}行')

task2

with open('data2.txt', 'r', encoding = 'utf-8') as f:
    data = f.read().split('\n')

unique_line = []
for line in data:
    if data.count(line) == 1:
        unique_line.append(line)

print(f'共{ len(unique_line) }独特行')
for i in unique_line:
    print(i)

 

 task3

import csv  # 导入csv模块

ls = [ ['城市', '大致人口'],
       ['南京', '850万'],
       ['纽约', '2300万'],
       ['东京', '3800万'],
       ['巴黎', '1000万'] ]

with open('data4.csv', 'w', encoding = 'utf-8', newline = '') as f:
    writer = csv.writer(f)  # 使用csv模块的writer类创建对象
    writer.writerows(ls)


# 从data4.csv中读出数据,把逗号替换成\t, 分行打印输出到屏幕上
with open('data4.csv', 'r', encoding = 'utf-8') as f:
    reader = csv.reader(f) # 使用csv模块的reader类创建对象
    for line in reader:
        print('\t'.join(line))

 

 任务5

class StudentDoc:
    def __init__(self,students_number,studens_name,major,python_score):
        self._students_number=students_number
        self._studens_name=studens_name
        self._major=major
        self._python_score=python_score

    def score(self):
        return print(f"{self._studens_name}的python课程分数{self._python_score}",end="\n")

    def modify_score(self,sore):
        self._python_score=sore

    def info(self):
        print(f"学号:{self._students_number}")
        print(f"姓名:{self._studens_name}")
        print(f"专业:{self._major}")

def main():
    a1=StudentDoc("202013170099","hhh","生态学",100)
    a2=StudentDoc("202013170000","yyy","生态学",0)

    a1.info()
    a2.info()

    a1.score()
    a2.score()

if __name__ == '__main__':
    print('模块信息: ', __doc__)
    print('StudentDoc类信息: ', StudentDoc.__doc__)
    main()

 

posted on 2021-05-31 21:20  许文德  阅读(26)  评论(0编辑  收藏  举报