import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['KaiTi']
with open(r'E:\pythoncoda\read\notebook.txt', encoding='utf-8') as file:
# contents = file.read()
# print(contents)
# 逐行输出,需要把上面两行注释掉,否则不能输出
# for line in file:
# print('line:', line)
# 第二种成行输出方法
contents = file.readlines()
print(contents)
newList = []
for content in contents:
newContent = content.replace('\n', '')
money = newContent.split(':')[-1]
newList.append(int(money))
print(newList)
x = [1, 2, 3, 4, 5, 6]
y = newList
plot = plt.plot(x, y, 'r', label='销售')
plt.xlabel('month')
plt.ylabel('money')
plt.savefig('销售额.png')
plt.legend()
plt.show()
# 求平均值
sum0 = 0
for money in newList:
sum0 += money
average = sum0 / len(newList)
print("average:", average)
average2 = sum(newList)/len(newList)
print("average2:", average2)
import matplotlib.pyplot as plt with open(r'E:\pythoncoda\read\notebook.txt') as file:
对txt文件进行读取,出现错误提示 ‘Uni

最低0.47元/天 解锁文章
19万+

被折叠的 条评论
为什么被折叠?



