我正在尝试使用python内置的.split和.strip函数从文件中获取标记内的数据。
file = open("testFile.txt")
for pointlessVariableName in file:
teamName = pointlessVariableName.strip().split("<name></team>")
for i in range(0,len(teamName)):
teamName[i] = teamName[i].strip() 我对这两个并不完全熟悉,但目标是去掉标签"“和"”,去掉标签中单词前后的空格。
发布于 2016-12-05 13:16:24
如果您的数据是格式良好的XML,请使用适当的XML解析器,使用split/strip解析结构化数据会很笨拙。
如果您仍然希望手动解析它,可以使用regex。
发布于 2016-12-05 13:15:51
您可以尝试以下代码块:
from w3lib.html import remove_tags
file = open("testFile.txt")
for pointlessVariableName in file:
teamName = map(str,remove_tags(pointlessVariableName).split())
print(teamName)输出:
['New', 'England', 'Patriots', 'Tom', 'Brady', '5-11']https://stackoverflow.com/questions/40966930
复制相似问题