如何用python将字符串拆分成多个子字符串?

时间:2018-03-14 07:10:04

标签: python string python-3.x split

我在文本文件中有这些行。第一行是列名。数据不是固定格式,所以我不能通过切割索引范围的字符串来拆分它。

a                    b              c            d             e               f              g          h           i          j
SEMESTER            24             18                          0               74            28          0          74        VISU
Burf               13+1/2         9+5/8                         0             1,122          320          0         1,122      VISU
1ST                8+3/4          4+1/2                                      8,192         1,280       3,570       8,192       CBL
Burf              13+1/2         9+5/8           36            0             1,235          592          0        1,235       VISU
SEMESTER            26             16           Line           0              160           212           0         160       CALC
                   8+3/4            7            26            0             8,243          734         970       8,243       CBL
OPEN WORK          6+1/8                      WiseLine        8243           10,239                                               
SEMESTER            19             16         line pipe        0              105             0           0         105       CALC
SEMESTER                           16          65#            0                             100          0         89         CALC
2ND                 6+1/8          4+1/2                        0             9,257
SEMESTER            20              16          65#             0            89             100          0         89         CALC
2ND               6+1/8           4+1/2                         0           9,257

我想分割所有的行并需要这样的输出。

["SEMESTER", "24", "18", "", "0", "74", "28", "0", "74", "SUVI"]
["BURF", "13+1/2", "9+5/8", "", "0", "1122", "320", "0", "1122", "SUVI"] 
["1ST", "8+3/4", "4+1/2", "", "", "8192", "1280", "3570", "8192", "BCL"]
["BURF", "13+1/2", "9+5/8", "36", "0", "1235", "592", "0", "1235", "SUVI"]
["SEMESTER", "26", "16", "Line", "0", "160", "212", "0", "160", "CALC"]
["", "8+3/4", "7", "26", "0", "8243", "734", "970", "8243", "BCL"]
["OPEN WORK", "6+1/8", "", "Wiseline", "8243", "10239", "", "", "", ""]
["SEMESTER", "19", "16", "line wise", "0", "105", "0", "0", "105", "CALC"]
["SEMESTER", "", "16", "65#", "0", "", "100", "0", "89", "CALC"]
["2ND", "6+1/8", "4+1/2", "", "0", "9257", "", "", "", ""]
["SEMESTER", "20", "16", "65#", "0", "89", "100", "0", "89", "CALC"]
["2ND", " 6+1/8", "4+1/2", "", "0", "9257", "", "", "", ""]

如果有人能帮我解决这个问题,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

以下代码将用于此目的(使用正则表达式),

import re

file_in = open('test_file.txt','r')
for line in file_in:
    new_line = re.sub("\s+", ",",line.strip())
    print [new_line]

它提供以下输出,

['a,b,c,d,e,f,g,h,i,j']
['SEMESTER,24,18,0,74,28,0,74,VISU']
['Burf,13+1/2,9+5/8,0,1,122,320,0,1,122,VISU']
['1ST,8+3/4,4+1/2,8,192,1,280,3,570,8,192,CBL']
['Burf,13+1/2,9+5/8,36,0,1,235,592,0,1,235,VISU']
['SEMESTER,26,16,Line,0,160,212,0,160,CALC']
['8+3/4,7,26,0,8,243,734,970,8,243,CBL']
['OPEN,WORK,6+1/8,WiseLine,8243,10,239']
['SEMESTER,19,16,line,pipe,0,105,0,0,105,CALC']
['SEMESTER,16,65#,0,100,0,89,CALC']
['2ND,6+1/8,4+1/2,0,9,257']
['SEMESTER,20,16,65#,0,89,100,0,89,CALC']
['2ND,6+1/8,4+1/2,0,9,257']