# coding:utf-8
# coding:unicode_escape
import re
import codecs
# 在打开一个文件读取的时候,再同时打开一个文件来写入。
# 通过反斜杠来对过长的代码行进行拆分,
# 但要注意在\后面不要有任何字符,包括空格。
with codecs.open("movies.txt", "r", encoding="utf-8") as f, \
codecs.open("data.txt", "w", encoding="utf-8") as out:
for line in f:
new_line = line.replace("\xa0", " ").strip()
#获取排名跟标题
temp = new_line.split()
ranking = temp[0]
title = temp[1]
# 获取电影年份
matched = re.search('\s+(\d{4})(\s|\()', new_line)
year = matched.group(1)
# 通过一个稍微复杂的正则来获取电影的国家,标签,评分以及评价人数
matched = re.match(".+/\s(.+)\s/\s(.+?)\s+(\d\.\d)\s+(\d+)人评价", new_line)
country, tag, rating, comment = matched.group(1, 2, 3, 4)
# 每个数据使用逗号分隔组成一行,保存到文件data.txt里
print("{},{},{},{},{},{},{}".format(ranking,title,rating, year,country,tag,comment),file=out)
python中的中文问题
最新推荐文章于 2024-08-10 10:09:09 发布