Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In a bit of a hurry, so probably didn't make this as clear as I could have. I also took some liberties in assuming your data is fixed width, as your sample was. Let me know if you have any questions.

current_branch = None
data = []
# Read data
with open("data.txt") as f:
    for l in f:
        if l[:11] == "SENSITIVITY":
            current_branch = l[44:88]
        elif l[:2] == "<-":
            continue
        elif len(l) > 2:
            assert current_branch is not None
            data.append( [l[:20], current_branch, l[20:]] )

# Strip out white space
data = [map(lambda x: x.strip(), l) for l in data]

# Create lines
data = [','.join(l) for l in data]

# Display file
print '\n'.join(data)

In a bit of a hurry, so probably didn't make this as clear as I could have. I also took some liberties in assuming your data is fixed width, as your sample was. Let me know if you have any questions.

current_branch = None
data = []
# Read data
with open("data.txt") as f:
    for l in f:
        if l[:11] == "SENSITIVITY":
            current_branch = l[44:88]
        elif l[:2] == "<-":
            continue
        elif len(l) > 2:
            assert current_branch is not None
            data.append( [l[:20], current_branch, l[20:]] )

# Strip out white space
data = [map(lambda x: x.strip(), l) for l in data]

# Create lines
data = [','.join(l) for l in data]

# Display file
print '\n'.join(data)

Output:

10000 LA    275.00,10000 LA    275.00 TO  20000 NY    275.00 1,0.1041
20000 NY    275.00,10000 LA    275.00 TO  20000 NY    275.00 1,-0.7613
30000 WDC   275.00,10000 LA    275.00 TO  20000 NY    275.00 1,-0.2254
40000 SF    275.00,10000 LA    275.00 TO  20000 NY    275.00 1,-0.2254
50000 AZ    275.00,10000 LA    275.00 TO  20000 NY    275.00 1,-0.2144
20000 NY    275.00,40000 SF    275.00 TO  50000 AZ    275.00 1,0.7613
30000 WDC   275.00,40000 SF    275.00 TO  50000 AZ    275.00 1,0.2254
40000 SF    275.00,40000 SF    275.00 TO  50000 AZ    275.00 1,0.2254
50000 AZ    275.00,40000 SF    275.00 TO  50000 AZ    275.00 1,0.2144
10000 LA    275.00,40000 SF    275.00 TO  60000 PD    275.00 1,0.1613
30000 WDC   275.00,40000 SF    275.00 TO  60000 PD    275.00 1,0.3254
40000 SF    275.00,40000 SF    275.00 TO  60000 PD    275.00 1,0.1540
50000 AZ    275.00,40000 SF    275.00 TO  60000 PD    275.00 1,0.3244