split the list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • syoung123
    New Member
    • Mar 2015
    • 1

    split the list

    hi,
    I have a list which is read from a file
    Code:
    ['48998.tyrone-cluster;gic1_nwgs;mbupi;18:45:44;R;qp32\n', '48999.tyrone-cluster;gic2_nwgs;mbupi;0;Q;batch\n', '49005.tyrone-cluster;...01R-1849-01_2;mcbkss;00:44:23;R;qp32\n', '49032.tyrone-cluster;gaussian_top.sh;chemraja;0;Q;qp32\n', '49047.tyrone-cluster;jet_egrid;asevelt;312:33:0;R;qp128\n', '49052.tyrone-cluster;case3sqTS1e-4;mecvamsi;0;Q;qp32\n', '49053.tyrone-cluster;...01R-1850-01_1;mcbkss;0;Q;batch\n', '49054.tyrone-cluster;...01R-1850-01_2;mcbkss;0;Q;batch\n']
    i need to get the output in the form
    Code:
    ['48998','18:45:44','R','qp32']
    ['48999','0','Q','batch']
    ['49005','00:44:23','R','qp32']
    and each of this list is written into a file job.txt
    till now i have tried split(".") but could not get the required output
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    It appears your data is consistently formatted. A for loop or nested list comprehension should do it.
    Code:
    parsed_data = [[item[0].split(".")[0], item[3], item[4], item[5].strip()]
                   for item in [s.split(";") for s in data]]
    Code:
    >>> print "\n".join([",".join(item) for item in parsed_data])
    48998,18:45:44,R,qp32
    48999,0,Q,batch
    49005,00:44:23,R,qp32
    49032,0,Q,qp32
    49047,312:33:0,R,qp128
    49052,0,Q,qp32
    49053,0,Q,batch
    49054,0,Q,batch
    >>>

    Comment

    Working...