需要帮助来解析文件 - python

我们有一个包含以下信息的文件:

Files: *
License: LGPL-2+
Files: po/bg.po
       po/eo.po
       po/sl.po
       po/sv.po
       po/th.po
       po/tr.po
License: LGPL-2+
Files: po/te.po
License: LGPL-2+
Files: po/vi.po
License: LGPL-2+
Files: tls/tests/mock-pkcs11.*
License: LGPL-2.1+
Files: dbus-1/dconf-dbus-1.*
       tests/gsettings.c tests/dbus1.c
License: GPL-3+
Files: debian/*
License: GPL-3
Files: po/fi.po
License: LGPL-2+

我想合并具有相同许可证的所有文件,以以下格式查找输出:

LGPL-2+(*,po/bg.po,po/eo.po,po/sl.po,po/sv.po,po/th.po,po/tr.po,po/fi.po,po/te.po,po/vi.po),LGPL-2.1+(tls/tests/mock-pkcs11.*),
GPL-3+(dbus-1/dconf-dbus-1.*,tests/gsettings.c tests/dbus1.c),GPL-3(debian/*)

- - 要么 - -

LGPL-2+(*,po/bg.po,po/eo.po,po/sl.po,po/sv.po,po/th.po,po/tr.po,po/fi.po,po/te.po,po/vi.po)
LGPL-2.1+(tls/tests/mock-pkcs11.*)
GPL-3+(dbus-1/dconf-dbus-1.*,tests/gsettings.c tests/dbus1.c)
GPL-3(debian/*)

任何帮助表示赞赏。谢谢!

参考方案

awk营救:

$1=="License:"{
  a[$2]=a[$2](a[$2]?",":"")f;
  next
}
$1=="Files:"{
  f=$2
  next
}
{
  for(i=1;i<=NF;i++)
    f=f (f?",":"")$i
}
END{
  for(i in a)
    printf "%s(%s)\n",i,a[i]
}

变量f用文件名填充。找到关键字License:时,变量f中的所有文件都将复制到数组a中。

END语句仅显示数组的内容。

请注意(...?...:...),这是内联C样式if ... then ... else ...语句,用于在每个文件名之间添加逗号,

如何从python向终端(linux)发送多个命令? - python

我想向Linux终端发送命令以运行python脚本。我有一个要运行的python文件列表,当我们顺序阅读该列表时,我想一个接一个地运行它们。第一个文件完成后,应发送第二个文件以运行,依此类推。 参考方案 您可以使用以下命令顺序运行脚本:python script1.py && python script2.py && pyth…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何在python中的某些行中存储变量? - python

我有以下代码:with open("/etc/network/interfaces", "r") as file: content = file.read() print content 它正在工作并显示以下内容:如何在变量中存储任何单词并打印该单词? 参考方案 with open("/etc/network…

在当前目录下编写一个脚本,生成多个目录,创建空文件,然后列出所有文件。 - python

Closed. This question needs details or clarity。它当前不接受答案。                                                                                                                            …