• python编程题3


    1. 将一个文件中的所有英文字母转换成大写,复制到另一文件中。

    1. fi=open("ex01.py",'r')
    2. fo=open("f2.txt",'w')
    3. for line in fi:
    4. line=line.upper()
    5. fo.write(line)
    6. fi.close()
    7. fo.close()

    2. 将一个文件中的指定单词删除后,复制到另一个文件中。

    1. fi=open("ex02.py",'r')
    2. fo=open("f2.txt",'w')
    3. deleteword="line"
    4. for line in fi:
    5. line1=line.replace(deleteword,"")
    6. #print(line1)
    7. fo.write(line1)
    8. fi.close()
    9. fo.close()

    3. 从键盘输入一个整数,求100除以它的商,并显示。要求对从键盘输入的数值进行异常处理。

    1. n=eval(input("请输入数值:"))
    2. try:
    3. s=100/n
    4. print(s)
    5. except Exception as ee:
    6. print(ee)

    4. 爬虫案例:爬取2024高校排名信息。

    1. from urllib.request import urlopen
    2. import bs4
    3. import matplotlib.pyplot as plt
    4. import csv
    5. def getHtml(url):
    6. try:
    7. response = urlopen(url)
    8. # print(response)
    9. return response.read().decode('utf-8')
    10. except:
    11. return ""
    12. def getUnivInfo(csvfile, html):
    13. soup = bs4.BeautifulSoup(html, "html.parser")
    14. csv_writer = csv.writer(csvfile)
    15. for tr in soup.find('tbody').children:
    16. if isinstance(tr, bs4.element.Tag):
    17. tds = tr('td')
    18. l1=tds[1].find('a').string
    19. # print(tds[2].prettify().splitlines())
    20. l2=tds[2].prettify().splitlines()[1][1:]
    21. #
    22. # print(l2)
    23. l3=tds[5].string
    24. csv_writer.writerow([tds[0].div.string.rstrip().lstrip(),l1,l2,l3])
    25. def printTopUniv(csvfile, num):
    26. fmt = "{0:^4}\t{1:{3}<10}\t{2:^5}"
    27. print(fmt.format("排名", "学校", "总分", chr(12288)))
    28. csvfile.seek(0, 0)
    29. recs = csv.reader(csvfile)
    30. for rec in recs:
    31. print(fmt.format(rec[0], rec[1], rec[3], chr(12288)))
    32. if int(rec[0]) == num:
    33. break
    34. def showUnivDictr(csvfile, num):
    35. udict = {}
    36. csvfile.seek(0, 0)
    37. recs = csv.reader(csvfile)
    38. for rec in recs:
    39. print(rec)
    40. udict[rec[2]] = udict.get(rec[2], 0) + 1
    41. if int(rec[0]) == num:
    42. break
    43. sort_udict = dict(sorted(udict.items(), key=lambda item: item[1], reverse=True))
    44. plt.rcParams['font.family'] = 'Simhei'
    45. results = [0 for i in range(6)]
    46. plt.title('中国前100名大学分布')
    47. plt.xlabel('省/直辖市')
    48. plt.ylabel('大学数量')
    49. plt.xticks(range(1, len(sort_udict) + 1), sort_udict.keys())
    50. plt.bar(range(1, len(sort_udict) + 1), sort_udict.values(), width=0.6)
    51. plt.savefig('univ_visua12.png')
    52. plt.show()
    53. def main():
    54. csvfile = open('univ_rank.csv', 'w+', newline='')
    55. url='https://www.shanghairanking.cn/rankings/bcur/202411'
    56. html = getHtml(url)
    57. getUnivInfo(csvfile, html)
    58. printTopUniv(csvfile, 30)
    59. showUnivDictr(csvfile, 100)
    60. csvfile.close()
    61. main()

  • 相关阅读:
    论文阅读:Duplex Contextual Relation Network for Polyp Segmentation
    uniapp本地打包详细步骤
    Dubbo 提供者与消费者的实现
    「实战应用」如何用DHTMLX将上下文菜单集成到JavaScript甘特图中(一)
    【DRAM存储器四】DRAM存储器的架构演进-part1
    10大领域5大过程47子过程快速记忆
    基于python的对比度增强(线性变换、直方图正规化、直方图均衡化、CLAHE)
    系统架构设计师学习笔记——软件架构设计_重点备忘录
    mysql分组排序并取每组的前1条记录
    服务器百万并发的原理与实现
  • 原文地址:https://blog.csdn.net/m0_47449012/article/details/140051628