【问题标题】:How can I split my file containing file names and information into multiple files separately?如何将包含文件名和信息的文件分别拆分为多个文件?
【发布时间】:2020-07-05 00:17:10
【问题描述】:

我有一个看起来像这样的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 命令(如 awkpython)来实现所需的输出?

注意:

  • 文件名没有任何空格或制表符,但它们可能包含-
  • 标题包含空格。
  • 行是制表符分隔的。
  • 真正的分隔符应该是文件名,因为我可以有多个标题。

提前致谢。

【问题讨论】:

  • 是否保证除了文件名行之外的所有行都包含空格或制表符?
  • @slin 是的,这是有保证的。
  • 是否给定文件名,例如NEDD4L,曾经在输入中重复还是只出现一次?
  • @EdMorton 它只出现一次,因为它是一个基因名称。

标签: python regex file awk split


【解决方案1】:
awk 'NF==1{filename=$0 ".txt"};{print > filename}' file.txt

一个等效但更高尔夫球的选择是

awk 'NF==1{f=$0".txt"}{print>f}' file.txt

【讨论】:

  • 我总是对 awk 的所作所为印象深刻...感谢您的回答!
  • 不客气!是的,用 awk 将如此重要的任务折叠成一行真是太棒了。
  • 除非您使用 GNU awk,否则在创建十几个输出文件后会出现“打开的文件过多”错误。如果您希望它具有可移植性并适用于大量输出文件,那么您需要随时 close() 它们:'NF==1{close(filename); filename=$0 ".txt"} {print &gt; filename}'
  • @Zen 便携式我的意思是“可以与其他 awk 一起使用”。现在您必须使用 GNU awk,因为它没有失败,但是如果您尝试在其他平台上使用您的脚本或在当前平台上使用其他 awk,那么它将失败。为避免这种可能性,您需要做的就是 close() 输出文件,就像我在评论中显示的那样。
  • @EdMorton 哦,好吧!感谢您的解释!
【解决方案2】:

这就是我想出的解决方案。它首先打开您要拆分的文件。然后它读入第一行,这是第一个文件的文件名。现在让我跳过 while 循环。它使用刚读入的文件名打开一个新文件(strip() 是消除行尾的换行符所必需的)。然后读取行并将它们写入新文件,直到新文件出现,其中没有空格或制表符。然后重复该过程,直到文件不再有要读取的行(我之前跳过的 while 循环)。

希望对你有帮助:)

file = open("file.txt", "r")

new_filename = file.readline()
while new_filename:
   with open(new_filename.strip() + ".txt", "w") as new_file:
      new_file.write(new_filename)
      line = file.readline()
      while " " in line or "\t" in line:
         # still the same new file
         new_file.write(line)
         line = file.readline()
   # file ended so read in line was the filename of the next file
   new_filename = line

file.close()

【讨论】:

  • 感谢您提出的答案!我还是会选择awk,因为它是一个单行命令。
猜你喜欢
  • 2017-05-20
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
  • 1970-01-01
  • 2014-12-13
相关资源
最近更新 更多