【问题标题】:python removing lines from filepython从文件中删除行
【发布时间】: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


【解决方案1】:

你不需要使用正则表达式:

cntent = re.sub('<[^>]+>',"\n", str(cntent))
with open(os.path.join('/Users/Brian/Documents/test', titles), "wb") as f: 
    f.writelines(line for line in cntent.splitlines(True) if line.strip())

str.strip() 在字符串的开头和结尾去除空格(包括换行符)。对于仅包含空格的行,它将返回空字符串;被评估为假值。

str.splitlines with True 用于分割行,但不排除新行。

【讨论】:

  • 除了您在f.writelines 错过的缩进之外,此代码有效!谢谢!
  • @user3671459,你是什么意思我错过了缩进?
  • IndentationError: expected an indented block,现在我要弄清楚为什么我的所有文件的格式都不是正确的,但这就是我的问题:)!
【解决方案2】:

试试这个模式
^\s+ w/ m 选项
Demo

【讨论】:

    猜你喜欢
    • 2023-02-08
    • 2016-03-03
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    相关资源
    最近更新 更多