我如何按段拆分文本文件?

时间:2015-11-16 02:38:34

标签: python split

我有一个文本文件,基本上它是一堆字符,我将要做的事情,但我一直在分裂它。

它看起来像这样:

1 2 3 4 5 6 7 8 
9 10 11 12 13 14 

15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37

39 40
41 42

我想用"段落#34;然后从每一行开始。我知道如何阅读这个功能和所有内容,但我一直在努力的一切都没有工作,比如分开(' \ n \ n')。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

这实际上比你做得简单得多:

10. Read each line in and append them together as long 
    as the next line is not empty. 
    When you do encounter a blank line, 
    split the current "paragraph" with ` `.

20. Goto 10

答案 1 :(得分:1)

这看起来像你想要的那样:

txt=open("nums.txt").read()
[[x for x in ilist if len(x) > 0] for ilist in map(lambda x : x.split("\n"),txt.split("\n\n"))]

[['1 2 3 4 5 6 7 8 ', '9 10 11 12 13 14 '], ['15 16 17 18 19 20', '21 22 23 24 25 26 27', '28 29 30 31 32 33 34 35 36 37'], ['39 40', '41 42']]

如果你想要它们全部为整数,那么:

map (lambda x : map(lambda x :reduce (lambda z,y: z+[int(y)] if y.isdigit() else z,x.split(),[]),x),[[x for x in ilist if len(x) > 0] for ilist in map(lambda x : x.split("\n"),txt.split("\n\n"))])

给出O / P:

[[[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14]], [[15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27], [28, 29, 30, 31, 32, 33, 34, 35, 36, 37]], [[39, 40], [41, 42]]]