Search code examples
pythonline

reading behind from list


import numpy as np

filename = 'input1.txt'
output_file = 'output.txt'
delimiters = ',.?!'

with open(filename, 'r') as f:
    text = f.read()
for delimiter in delimiters:
    text = text.replace(delimiter, ',')
lines = text.split('\n')
parts = list()
print(lines)

I want all the lines containing text / numbers from the back to be in the sheet

example I have an input.text file

1,2,3,4,5,6
7,8,9,10,11,12
13,14,15,16,17

this give me

['1,2,3,4,5,6', '7,8,9,10,11,12', '13, 14,15,16,17 ',' ',' ',' ', '', '', '', '', '']

I'm looking for an exit from behind to read the lines

['13, 14,15,16,17', '7,8,9,10,11,12', '1,2,3,4,5,6', '', '', '', '', '', '', '']

Thank you for your help


Solution

  • Reverse a list in Python:

    print(list('abc')[::-1])
    > ['c', 'b', 'a']
    

    Your case:

    import numpy as np
    import re
    filename = 'input1.txt'
    output_file = 'output.txt'
    text = re.sub('[.?!]', ',', text)
    
    with open(filename, 'r') as f:
        text = f.read()
    text = re.sub('[.?!]', ',', text)
    
    lines = text.split('\n')[::-1]
    parts = []
    print(lines)