-4
LMIA = '''[LMIA Late Miami  101 0089        Sun 06 Sep 2015
LMIA Late Miami 267 8731        Sat 05 Sep 2015
LMIA Late Miami 178 7110        Fri 04 Sep 2015
LMIA Late Miami 645 3920        Thu 03 Sep 2015
LMIA Late Miami 233 9050        Wed 02 Sep 2015
LMIA Late Miami 982 7556        Tue 01 Sep 2015
LMIA Late Miami 588 9164        Mon 31 Aug 2015
LMIA Late Miami 203 6667        Sun 30 Aug 2015
LMIA Late Miami 013 2265        Sat 29 Aug 2015
LMIA Late Miami 979 5688        Fri 28 Aug 2015]'''
for item in LMIA:
    if item == LMIA[-1]:
        print(item + ',')
    if item is not item.isdigit():
        print(item.rstrip(','), end='')
    if item is int:
        print(item, end=',')
    else:
        print(end=',')

Here is what i did manually, to much lines to do manually

LMIA = [
    'Tue', 06, 'Oct', 2015, 684, 7695,
    'Mon', 05, 'Oct', 2015, 485, 0736,
    'Sun', 04, 'Oct', 2015, 534, 3967,
    'Sat', 03, 'Oct', 2015, 685, 9397,
    'Fri', 02, 'Oct', 2015, 531, 4950,
    'Thu', 01, 'Oct', 2015, 119, 8119,
    'Wed', 30, 'Sep', 2015, 287, 6712,
    'Tue', 29, 'Sep', 2015, 323, 3634,
    'Mon', 28, 'Sep', 2015, 793, 9046]
Andy
  • 49,085
  • 60
  • 166
  • 233

1 Answers1

0

I deleted the [] brackets in your input data, but otherwise it's essentially the same:

LMIA = """
    LMIA Late Miami 101 0089        Sun 06 Sep 2015
    LMIA Late Miami 267 8731        Sat 05 Sep 2015
    LMIA Late Miami 178 7110        Fri 04 Sep 2015
    LMIA Late Miami 645 3920        Thu 03 Sep 2015
    LMIA Late Miami 233 9050        Wed 02 Sep 2015
    LMIA Late Miami 982 7556        Tue 01 Sep 2015
    LMIA Late Miami 588 9164        Mon 31 Aug 2015
    LMIA Late Miami 203 6667        Sun 30 Aug 2015
    LMIA Late Miami 013 2265        Sat 29 Aug 2015
    LMIA Late Miami 979 5688        Fri 28 Aug 2015
"""

results = []
for line in LMIA.split('\n'):  # Just split on newlines
    if 'LMIA' not in line:  # Skip lines without data
        continue
    words = line.split()  # Split by spaces
    indices = [5, 6, 7, 8, 3, 4]  # The useful columns
    row = [words[i].strip() for i in indices]  # Keep only the words we want

    # A hacky way of converting our strings to actual integers
    ints = [1, 3, 4, 5]
    for i in ints:
        row[i] = int(row[i])

    results.append(row)

# Now we need to sort results by month, then day
months = {'Sep': 9, 'Aug': 8}  # Add other months as required

results = sorted(results, key=lambda x: int(x[1]))  # Sort by day first
results = sorted(results, key=lambda x: months[x[2]])  # Then by month

print(results)

This doesn't result in quite the same format as you wanted, but it gives a list of lists containing the parts you want:

[
    ['Fri', 28, 'Aug', 2015, 979, 5688],
    ['Sat', 29, 'Aug', 2015, 013, 2265],
    ['Sun', 30, 'Aug', 2015, 203, 6667],
    ['Mon', 31, 'Aug', 2015, 588, 9164],
    ['Tue', 01, 'Sep', 2015, 982, 7556],
    ['Wed', 02, 'Sep', 2015, 233, 9050],
    ['Thu', 03, 'Sep', 2015, 645, 3920],
    ['Fri', 04, 'Sep', 2015, 178, 7110],
    ['Sat', 05, 'Sep', 2015, 267, 8731],
    ['Sun', 06, 'Sep', 2015, 101, 0089]
]

You could then use these results to generate an actual CSV, if you wanted to, very easily.

supermitch
  • 2,062
  • 4
  • 22
  • 28
  • Thanks for your help, but when running the code I get error on row = [words[i].strip() for i in indices] # Keep only the words we want. IndexError: list index out of range. – user5325271 Oct 13 '15 at 13:03
  • It's telling you that one of the values in `indices` is too high, for the number of words. Try printing `words` before this line, and see how many there are. And then make sure you adjust `indices` accordingly. Maybe something is slightly different. Remember that the first element is 0, not 1. – supermitch Oct 13 '15 at 16:53