【发布时间】:2022-11-24 03:39:59
【问题描述】:
我在 python 上编辑 txt 文件时遇到问题。
嗨,大家好,
我在 python 上编辑 txt 文件时遇到问题。
这是txt文件的前几行
m0 +++$+++ 10 things i hate about you +++$+++ 1999 +++$+++ 6.90 +++$+++ 62847 +++$+++ ['comedy', 'romance']
m1 +++$+++ 1492: conquest of paradise +++$+++ 1992 +++$+++ 6.20 +++$+++ 10421 +++$+++ ['adventure', 'biography', 'drama', 'history']
这是我的代码:
import re
file = open('datasets/movie_titles_metadata.txt')
def extract_categories(file):
for line in file:
line: str = line.rstrip()
if re.search(" ", line):
line = re.sub(r"[0-9]", "", line)
line = re.sub(r"[$ + : . ]", "", line)
return line
extract_categories(file)
我需要得到一个看起来像这样的输出:
['action', 'comedy', 'crime', 'drama', 'thriller']
有人可以帮忙吗?
【问题讨论】: