##有没有人能帮我看看我的Python代码哪里有问题,我写好之后在我电脑上运行不了。
##这是题目,但是文件好像发不了,如果有人能帮我看一下的话,麻烦私信我,我看看能不能发文件给你
##以下是我写的代码
import pandas as pd
import matplotlib.pyplot as plt
import os
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
#第一题
def datamovies():
while True:
filename=input('请输入要打开的文件名tmdb_5000_movies.csv:')
try:
df=pd.read_csv(filename,encoding='ANSI')
print(df.head(3))
print(df.tail(2))
print("任务一执行成功!")
break
except:
print('任务一执行失败')
#第二题
def dataPreparing():
while True:
filename=input('请输入要打开的文件名tmdb_5000_movies.csv:')
try:
df=pd.read_csv(filename,encoding='ANSI')
df_new=pd.DataFrame(df,
colums=['Budget','original_language','release_data','popularity','title'])
df=df_new.dropna(inplace=True)
df.to_csv('tmdb_5000_movies_budgt_popularity.csv',encoding='ANSI')
print("任务二执行成功!")
break
except:
print('任务二执行失败')
#第三题
def dataselection():
while True:
filename=input('请输入要打开的文件名tmdb_5000_movies_budgt_popularity.csv:')
try:
df_new=pd.read_csv(filename,encoding='ANSI')
df_newdata=df_new[df_new['original_language'=="e"]]
df_newdata.to_csv('tmdb_5000_movies_budgt_popularity_en.txt',\
encoding='',index=False)
print("任务三执行成功!")
break
except:
print('任务三执行失败')
#第四题
def datatransport():
while True:
filename=input('请输入要打开的文件名tmdb_5000_movies_budgt_popularity_en.txt:')
try:
df_newdata=pd.read_csv(filename,encoding='')
df_newdata.to_excel('tmdb_5000_movies_budgt_popularity_en.xlsx',\
encoding='',index=False)
print("任务四执行成功!")
break
except:
print('任务四执行失败')
#第五题
def dataVisualization():
while True:
filename=input('请输入要打开的文件名tmdb_5000_movies_budgt_popularity_en.txt:')
try:
df_newdata=pd.read_csv(filename,encoding='')
df_newdata.sort_values(by='title',ascending=False)
df_newdata=pd.read_txt(filename,encoding='')
df_newdata=df_newdata.loc[:,['Budget','title']]
plt.figure(figsize=(20,10))
plt.plot(df_newdata['Budget'],df_newdata['title'],label='Budget',color='red')
plt.plot('title',fontsize=12)
xlength=len(df_newdata)
print('xlength=',xlength)
xticksloc=[i for i in range(xlength)if i%5==0]
print('xticksloc=',xticksloc)
xtickslabels=df_newdata['title'].values[::5]
print('xtickslabels',xtickslabels)
plt.xticks(xticksloc,xtickslabels,rotation=45)
plt.ylabel('Budget',fontsize=12)
plt.legend(fontsize=14)
plt.title("电影预算",fontsize=16)
plt.savefig('movies_en_budget_2000_2010.png',dpi=400)
df_newdata=df_newdata.loc[:,['popularity','title']]
plt.figure(figsize=(20,10))
plt.plot(df_newdata['popularity'],df_newdata['title'],label='popularity',color='green')
plt.plot('title',fontsize=12)
xlength=len(df_newdata)
print('xlength=',xlength)
xticksloc=[i for i in range(xlength)if i%5==0]
print('xticksloc=',xticksloc)
xtickslabels=df_newdata['title'].values[::5]
print('xtickslabels',xtickslabels)
plt.xticks(xticksloc,xtickslabels,rotation=45)
plt.ylabel('popularity',fontsize=12)
plt.legend(fontsize=14)
plt.title("电影的受欢迎程度",fontsize=16)
plt.savefig('movies_en_popularity_2000_2010.png',dpi=400)
plt.show()
print("任务五执行成功!")
break
except:
print('任务五执行失败')
def menu():
print('【任务选择】\n'
'+------学生成绩数据分析及可视化系统------+\n'
'|0、返回。 |\n'
'|1、任务一:数据读取。 |\n'
'|2、任务二:数据预处理。 |\n'
'|3、任务三:数据选择并导出。 |\n'
'|4、任务四:数据转存。 |\n'
'|5、任务五:数据可视化。 |\n'
'+-----------------------------------------+')
def task():
while True:
menu()
num=input("请输入任务选项")
if num=='1':
datamovies()
elif num=='2':
if os.path.exists('studentScoreP.csv'):
dataPreparing()
else:
print('未能执行当前选项,请先执行前面的选项!')
elif num=='3':
if os.path.exists(''):
dataselection()
else:
print('未能执行当前选项,请先执行前面的选项!')
elif num=='4':
if os.path.exists(''):
datatransport()
else:
print('未能执行当前选择,请先执行前面的选项!')
elif num=='5':
if os.path.exists(''):
dataVisualization()
else:
print('未能执行当前选项,请先执行前面的选项!')
elif num=='0':
print('程序结束!')
break
else:
print('输入选项有误')
input("回车显示菜单")
if __name__=='__main__':
task()
运行有啥问题吗