我正在创建一个MapReduce作业以从XML文件中查找“ ArticleTitle”。我正在mapper.py上识别标签并根据字母进行拆分。 以下是脚本:
tree = ET.parse('File location')
doc = tree.getroot()
for ArticleTitle in doc.iter('ArticleTitle'):
file1 = (ET.tostring(ArticleTitle, encoding='utf8').decode('utf8'))
filename = file1[52:(len(file1))]
Article_Title= filename.split("<")[0]
# print(Article_Title)
for line in Article_Title:
line_1= re.findall(r"\w+|[^\w\s]", line, re.UNICODE)
print(line_1)
我得到的输出是:
['T']['h']['e'][]['e']['f']['f']['e']['c']['t'][]['o']['f']
但是,我希望输出为:
['The', 'effect', 'of', 'Hene', 'laser']
答案 0 :(得分:2)
Article Title
是一个字符串。参见:
Article_Title= filename.split("<")[0]
如果您遍历字符串,则将获得单个字符。
for i in "hello!":
print(i)
>>>>h
>>>>e
>>>>l
>>>>l
>>>>o
>>>>!
如果您想要整个单词,则不需要循环-只需Article_Title.split()
"The effect of Hene laser" --> ['The', 'effect', 'of', 'Hene', 'laser']