【发布时间】:2014-12-14 14:38:30
【问题描述】:
问题:
我正在尝试从我的 .txt 文件中删除空行。 因为我的 .txt 文件是 Python 通过 HTML 下载生成的,我想将它们保存在某个位置,所以我必须使用 Os.path.join。
这是在删除所有标签并仅保留标签内部后将 HTML 保存在该位置的代码:
cntent = re.sub('<[^>]+>',"\n", str(cntent))
with open(os.path.join('/Users/Brian/Documents/test',titles), "wb") as file:
file.writelines(str(cntent))
我怎样才能做到这一点?
文件的结果:
Productspecificaties
Uiterlijke kenmerken
Gewicht
185 g
我尝试了什么:
filtered = filter(lambda x: not re.match(r'^\s*$', x), original)
期望的结果
Productspecificaties
Uiterlijke Kenmerken
Gewicht
185Gr
请注意,在第一行代码 re.sub... 我使用“\n”,否则根本没有空格。
【问题讨论】:
-
也许像
'\n'.join([line.strip() for line in cntent.split() if line.strip() != ''])这样简单的东西?
标签: python regex string file str-replace