【问题标题】:How to fill the white-space with info while leaving the rest unchanged?如何用信息填充空白,同时保持其余部分不变?
【发布时间】:2017-10-27 07:30:34
【问题描述】:

我正在为飞行模拟器构建场景,需要弄清楚如何编辑文本文件中的多行(其中 3,579,189 行)。

我有 TextCrawler Pro、Node、Python SVN 和 Notepad++ 作为工具。

原始的预编辑部分:

POLYGON_POINT -79.750000000217,42.017498354525,0
POLYGON_POINT -79.750000000217,42.016478251402,0
POLYGON_POINT -79.750598748133,42.017193264943,0
POLYGON_POINT -79.750000000217,42.017498354525,0


POLYGON_POINT -79.750000000217,42.085882815878,0
POLYGON_POINT -79.750000000217,42.082008734634,0
POLYGON_POINT -79.751045507507,42.082126409633,0
POLYGON_POINT -79.750281907508,42.083166574215,0
POLYGON_POINT -79.750781149174,42.084212672130,0
POLYGON_POINT -79.750000000217,42.085882815878,0


POLYGON_POINT -79.750000000217,42.088955814831,0
POLYGON_POINT -79.750456566883,42.087544672125,0
POLYGON_POINT -79.751642899173,42.088273325249,0
POLYGON_POINT -79.751461052298,42.088916154415,0
POLYGON_POINT -79.750000000217,42.088955814831,0

使用 Notepad++ 的替换功能,添加POLYGON_POINT 行很容易。现在我需要一些帮助才能使它看起来像这样:

BEGIN_POLYGON
POLYGON_POINT -79.750000000217,42.017498354525,0
POLYGON_POINT -79.750000000217,42.016478251402,0
POLYGON_POINT -79.750598748133,42.017193264943,0
POLYGON_POINT -79.750000000217,42.017498354525,0
END_POLY
BEGIN_POLYGON
POLYGON_POINT -79.750000000217,42.085882815878,0
POLYGON_POINT -79.750000000217,42.082008734634,0
POLYGON_POINT -79.751045507507,42.082126409633,0
POLYGON_POINT -79.750281907508,42.083166574215,0
POLYGON_POINT -79.750781149174,42.084212672130,0
POLYGON_POINT -79.750000000217,42.085882815878,0
END_POLY
BEGIN_POLYGON
POLYGON_POINT -79.750000000217,42.088955814831,0
POLYGON_POINT -79.750456566883,42.087544672125,0
POLYGON_POINT -79.751642899173,42.088273325249,0
POLYGON_POINT -79.751461052298,42.088916154415,0
POLYGON_POINT -79.750000000217,42.088955814831,0

即在每个块之前添加BEGIN_POLYGON,在每个块之后添加END_POLY

我该怎么做?

【问题讨论】:

  • 您是在问如何在 Notepad++ 中执行此操作吗?还是蟒蛇?你提出的方法是什么?你试过什么?

标签: python notepad++


【解决方案1】:

我将使用itertools.groupby(仅采用if k 条件下的非空白组)对行进行分组,并为每个组添加页眉/页脚。然后使用itertools.chain 将组展平

import itertools

with open("file.txt") as f, open("fileout.txt","w") as fw:
    fw.writelines(itertools.chain.from_iterable([["BEGIN_POLYGON\n"]+list(v)+["END_POLYGON\n"] for k,v in itertools.groupby(f,key = lambda l : bool(l.strip())) if k]))

key = lambda l : bool(l.strip())) 是分组键:测试空行但测试行终止

此方法不需要完全读取文件,因此适用于非常大的文件。它逐行处理文件,因此不会占用内存。

【讨论】:

  • “你知道吗,你是我的 heeerrrooooo……”我欠你任何你想要的东西,亲爱的!
  • @BrianBash 不要忘记投票并接受对您有用的答案! stackoverflow.com/help/someone-answers
  • @Jean-FrançoisFabre 欢迎您,您认为我的回答在这里正确吗? stackoverflow.com/questions/46976157/…我是根据你的回答,但我不完全知道我是否理解了这个问题
【解决方案2】:

使用sed的快速解决方案

cat -s file.txt |\
    sed -e 's/^$/END_POLY\nBEGIN_POLYGON/'\
    -e '1i BEGIN_POLYGON'\
    -e '$a END_POLY'
  • cat -s 将所有空行压缩成一个
  • 第一个 sed 用 END_POLY 和 BEGIN_POLYGON 标签替换空行
  • 第二个也是最后一个 sed 将剩余的标签预先添加到输出中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多