python - 如何将包含文件名和信息的文件分别拆分为多个文件?
问题描述
我有一个file.txt
看起来像这样的(我删除了行以简化我的示例):
PLXNA3 ### <- filename1
Missense/nonsense : 13 mutations # <- header spaces
accession codon_change amino_acid_change # <- column names tsv
ID73 CAT-TAT His66Tyr # <- line tsv
ID63 GAC-AAC Asp127Asn # <- line tsv
ID31 GCC-GTC Ala307Val # <- line tsv
NEDD4L ### <- filename2
Splicing : 1 mutation # <- header spaces
accession splicing_mutation # <- column names tsv
ID51 IVS1 as G-A -16229 # <- line tsv
Gross deletions : 1 mutation # <- header spaces
accession DNA_level description HGVS_(nucleotide) HGVS_(protein) # <- column names tsv
ID853 gDNA 4.5 Mb incl. entire gene Not yet available Not yet available # <- line tsv
OPHN1 ### <- filename3
Small insertions : 3 mutations # <- header spaces
accession insertion HGVS_(nucleotide) # <- column names tsv
ID96 TTATGTT(^183)TATtCAAATCCAGG c.549dupT p.(Gln184Serfs*23) # <- line tsv
ID25 GTGCT(^310)AAGCAcaG_EI_GTCAGTTCT c.931_932dupCA # <- line tsv
我想拆分此文件以获得 3 个不同的文件:
PLXNA3.txt
PLXNA3 ### <- filename1
Missense/nonsense : 13 mutations # <- header spaces
accession codon_change amino_acid_change # <- column names tsv
ID73 CAT-TAT His66Tyr # <- line tsv
ID63 GAC-AAC Asp127Asn # <- line tsv
ID31 GCC-GTC Ala307Val # <- line tsv
NEDD4L.txt
NEDD4L ### <- filename2
Splicing : 1 mutation # <- header spaces
accession splicing_mutation # <- column names tsv
ID51 IVS1 as G-A -16229 # <- line tsv
Gross deletions : 1 mutation # <- header spaces
accession DNA_level description HGVS_(nucleotide) HGVS_(protein) # <- column names tsv
ID853 gDNA 4.5 Mb incl. entire gene Not yet available Not yet available # <- line tsv
OPHN1
OPHN1 ### <- filename3
Small insertions : 3 mutations # <- header spaces
accession insertion HGVS_(nucleotide) # <- column names tsv
ID96 TTATGTT(^183)TATtCAAATCCAGG c.549dupT p.(Gln184Serfs*23) # <- line tsv
ID25 GTGCT(^310)AAGCAcaG_EI_GTCAGTTCT c.931_932dupCA # <- line tsv
如何使用任何 linux 命令(如awk
or )来实现所需的输出python
?
笔记:
- 文件名没有任何空格或制表符,但它们可能包括
-
. - 标头包含空格。
- 行是制表符分隔的。
- 真正的分隔符应该是文件名,因为我可以有多个标题。
提前致谢。
解决方案
awk 'NF==1{filename=$0 ".txt"};{print > filename}' file.txt
一个等效但更具高尔夫球性的选择是
awk 'NF==1{f=$0".txt"}{print>f}' file.txt
推荐阅读
- arrays - 关于动态内存分配的几个问题
- performance - 使用 FAST 有没有办法将样式应用于子 ShadowDOM 元素?
- sql - 如何连接多行的结果
- c# - C# - 多个 switch 案例/语句与嵌套 If
- azure-cognitive-search - 在 Azure 搜索中对作为集合的子实体执行 geo.distance 过滤器
- python - 如何将百分比转换为时间序列索引?
- javascript - 如何使用光标悬停来更改幻灯片而不是单击?当光标在幻灯片上时,我正在尝试更改幻灯片
- python - 如何使用 'q' 调用 API 并在 Django 视图中获得响应?
- angularjs - 使用 ui-router 处理嵌套状态
- botframework - 自适应卡 Action.OpenUrl 不适用于 Microsoft Teams 机器人中的某些 URL