一个英文的作业
本帖最后由 帅哥小C啊 于 2022-12-6 11:02 编辑本帖最后由 lxping 于 2022-12-4 20:23 编辑
将代码文件和films.txt放在同一个文件夹里面,运行就可以了
def loadFilms():
films = []
i = 0
with open("films.txt") as f:
print(f.read()) #打印films.txt的文件内容
f.seek(0) #指针回到文件初始位置
for each in f.readlines():
i += 1
filmname = each.split(",")
filmmark = float(each.split(","))
filmlist =
films.append(filmlist) #读取films.txt每一行后,按照格式添加到films列表中
return films
def printFilms(films):
print(films)
def addFilms(films):
for i in films:
i += 1 #增加的film是放在最前面的,所以已在列表中的film的序号既rank要调整
filmrank = 1
filmname = input("Please input film name:")
filmmark = float(input("Please input film mark:"))
films.insert(0, ) #在列表第一个位置插入新增信息
return films
def saveFilms(films):
with open("newfilms.txt", "w+") as n: #创建新文件
for i in films:
n.writelines(str(i) + "," + i + "," + str(i)+ "\n") #每次读取一行,写入一行
if __name__ == "__main__":
films = loadFilms()
films = addFilms(films)
saveFilms(films)
printFilms(films)
= RESTART: C:/Users/Administrator/Desktop/python/homework/once a week/newfilms.py
1,The Shawshank Redemption,9.3
2,The Godfather,9.2
3,The Dark Knight,9.0
Please input film name:Farewell My Concubine
Please input film mark:9.6
[, , , ] 本帖最后由 suchocolate 于 2022-12-4 20:28 编辑
import csv
def loadFilms():
films = []
with open('films.txt', 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
_lst = list(row)
_lst = float(_lst)
films.append(_lst)
return films
def printFilms(films):
for film in films:
print(f'Film {film} - {film} - {film}')
def addFilm(films):
film_name = input('Please input film name:')
film_mark = float(input('Please input film mark:'))
films.append()
def sortFilm(films):
films.sort(key=lambda x: x, reverse=True)
for i in range(len(films)):
films = i + 1
def saveFilms(films):
sortFilm(films)
print(films)
with open('newfilms.txt', 'w', encoding='utf-8', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(films)
if __name__ == "__main__":
film_list = loadFilms()
printFilms(film_list)
addFilm(film_list)
sortFilm(film_list)
saveFilms(film_list)
用 pandas 写写? ^_ import pandas as pd
# a) load the films into a database from the file (films.txt) and return it
# films = loadFilms()
def loadFilms():
df = pd.read_csv('films.txt', sep=',', header=None, index_col=0)
return df.to_numpy().tolist()
films = loadFilms()
print(films)
# b) list all the films in the database
# printFilms(films)
def printFilms(films):
for i, row in enumerate(films, start=1):
row.insert(0, i)
print(','.join(map(str, row)))
printFilms(films)# films = loadFilms()
# c) add a film to the database
# films = addFilm(films)?? 为啥是复数?
def addFilm(films=None):# 增加"若干条电影信息"(二维列表)
if films is not None \
and isinstance(films, list) and isinstance(films, list):
return loadFilms() + films
else:
pass
films = addFilm(films=[["Farewell My Concubine", 9.6]])
# d) save the films in a database to file (newfilms.txt)
# saveFilms(films)
def saveFilms(films):
df = pd.DataFrame(films)
df.columns = ['name', 'mark']
df.sort_values(by='mark', ascending=False, inplace=True)
df.index = range(1, len(df.index)+1) # as the rank
df.to_csv('newfilms.txt', index=True, header=None)
saveFilms(films=films)
# Your program should follow the test case with same input and output.
# ?? 难道要还有写 测试用例?
import csv
def loadFilms():
films = []
with open('films.txt', 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
_lst = list(row)
_lst = float(_lst)
films.append(_lst)
return films
def printFilms(films):
for film in films:
print(f'Film {film} - {film} - {film}')
def addFilm(films):
film_name = input('Please input film name:')
film_mark = float(输入('请输入胶片标记:'))
films.append()
def sortFilm(films):
films.sort(key=lambda x: x, reverse=True)
对于范围内(镜头(胶片)):
电影 = i + 1
def saveFilms(films):
排序电影(电影)
印刷(电影)
使用 open('newfilms.txt', 'w', encoding='utf-8', newline='') 作为 csvfile:
writer = csv.writer(csvfile)
作家.编剧(电影)
如果__name__ == “__main__”:
film_list = loadFilms()
印刷胶片(film_list)
添加电影(film_list)
排序电影(film_list)
保存电影(film_list)
谢谢大家了,问题基本上已经解决了。(奖励只能发一份嘛,我就给了最早回复的那位朋友了) 前来学习。
页:
[1]