# This is a sample Python script. # Press Shift+F10 to execute it or replace it with your code. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. file= open("jiangsu.txt", 'r+') l = file.read() s = l.split('\n') n1= [] b = [] total = 0.0 for j in s: n1.append(list(j.split(' '))) n = len(n1) for i in range(0, n): total = total + float(n1[i][1]) total = "{:.2f}".format(total) j= str(total) average = float("{:.2f}".format(float(total) / (n))) a = str(average) for i in range(0, n): if float(n1[i][1]) < average: b.append(n1[i]) m = len(b) for i in range(0, m): b[i][1] = float(b[i][1]) file.write("\nTotal is ") file.write(j) file.writelines("\nAverage is ") file.write(a) file.write("\nCities below average are:") for item in b: file.write("\n") file.write(str(item)) # See PyCharm help at https://www.jetbrains.com/help/pycharm/
读文件并且追加文件
最新推荐文章于 2022-12-15 21:20:00 发布