简体   繁体   中英

Line split when multiple white spaces in python

I looked though many similar questions, however my separators are not special characters such as "\" or "*", therefore none of the solutions worked. I am writing my results to a file in python, and re-opening it to read and process.

file1.txt
control1
1  10      12
1  34      44
2   1      -3
control2
3   4     -10.3
3   3.390   4

I separate each entry until I see a line having 'control' into chapters :

import re
import sys, string, glob, os
with open('file1.txt') as f:
        with open("control_output.txt", "w") as output:
            mytext = f.read()
            chapter = re.split("control[0-2]+\n", mytext)
            i=1
            print chapter[i]
            output.write(chapter[i])
            for filename in glob.glob(os.path.join(filePath, 'control_output.txt')):
                 merged_table=open(filename,'r')
                 for line in merged_table:
                      line = line.strip().split('\t')
                      print line

However it prints nothing, as the line does not have a tab separator. If I go out of the script before reading the file, and change all whitespaces to tabs, then it works:

sed -i 's/ \+ /\t/g' control_output.txt 

Then I have the output:

['1', '10', '12']
['1', '34', '44']
['2', '1', '-3']

I also tried with subprocess.call however

subprocess.call(["sed", "-i",  's/ \+ /\t/g', "control_output.txt"])

Then I have the output of:

[[]]

I tried re.split with multiple white spaces:

line = re.split(r'\s*', line)

Which also gave

[[]]

However the expected output should be:

['1', '10', '12']
['1', '34', '44']
['2', '1', '-3']

How do I split string with multiple delimiters?

for line in merged_table:
    line = line.strip().split()
        print line

This will split on all whitespace and not just tabs

import re
import sys, string, glob, os
with open('file1.txt') as f:
    with open("control_output.txt", "w") as output:
        mytext = f.read()
        chapter = re.split("control[0-2]+\n", mytext)
        i=1
        print chapter[i]
        output.write(chapter[i])

    # You should move this code block out of with open("control_output.txt", "w") as output:
    for filename in glob.glob(os.path.join(filePath, 'control_output.txt')):
        with open(filename, 'r') as f_table:
            merged_table = f_table.readlines()

        for line in merged_table:
            line = re.split('\s+', line.strip())
            print line

Hope to help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM