二、豆瓣电影数据的整理
import os
import csv
def readAllFiles(filePath):
id = []
name = []
fileList = os.listdir(filePath)
for file in fileList:
path = os.path.join(filePath, file)
if os.path.isfile(path):
with open(path, 'r') as f:
reader = csv.reader(f)
for i in reader:
if str(i[4]) != "NONE" and str(i[4]) != 'id':
id.append(i[4])
name.append(i[7])
return name, id
#这里的路径是你当前目录下的路径,看下图解释
movie_name, movie_id = readAllFiles("data")
with open('index.csv', 'w', encoding='utf-8-sig', newline='') as f:
f1 = csv.writer(f)
for i in range(len(movie_name)):
f1.writerow([movie_id[i], movie_name[i]])
谢谢大家的阅读!