Vertically aligned str to reversed horizontal sequence

Viewed 68

Once I get all the strings that I need, the next step would be to locate all the vertically aligned string integers and put them next to each other in the same line instead of each str to be in separate and later reverse them

text file:

night train whistles stars
over a nation under
mad temporal czars

round lumps of cells grow:

1234567     Info1 
1234567     Info2 
1234567     Info3 

5
2
7
0
1
5
8

3
2
7
0
1
5
8

9
6
1
7
4
5
8

1
9
7
0
1
5
8

8
9
7
2
4
5
8

9
9
7
2
4
5
8

Info5
Info6

Desired output:

8510725 
8510723
8547169
8510791
8542798
8542799

code:

ifile = open('pgone.txt','r')
buffer = ifile.readlines()
temp = ""
listy = []

for e in buffer:
    temp += e.strip('""')
    if len(e) == 2:
        listy.append(e)


one = listy[0:7]
two = listy[7:14]
three = listy[14:21]
four = listy[21:28]
five = listy[28:35]
six = listy[35:]

one.reverse()
two.reverse()
three.reverse()
four.reverse()
five.reverse()
six.reverse()

Now of course I get the desired result separately in this manner, however, I would be interested to get more elegant solutions to this problem. Thanks!

3 Answers

You can loop through each line of the file and if the line without newline is a digit, and only one digit then add it to our numbers string. If we reach a new line check if the numbers string has any data, reverse it and store it in our list of found numbers, then reset numbers string to be empty.

also this solution is not hardcodeing the amount of numbers to find and which lines to look on. It should scale regardless of how many numbers were in the file.

numbers = []
with open("out2.txt") as file:
    number = ""
    for line in file:
        content = line.strip()
        if content.isdigit() and len(content) == 1:
            number += content
        elif content == "" and number != "":
            numbers.append(number[::-1])
            number = ""
print(*numbers, sep="\n")

OUTPUT

8510725
8510723
8547169
8510791
8542798
8542799

This code below will do the trick:

numbers = []
numbers_converted = []

with open('txtfile.txt','r') as f:
    for line in f.readlines():
        if len(line) == 2:
            numbers.append(line.replace('\n', ''))

    numbers = [numbers[i*7:i*7+7] for i in range(7)] #The word is 7 char big
    numbers.pop() #Get rid of the last list because it is empty

    for number in numbers:
        number.reverse()

        numbers_converted.append(int("".join(number)))

print(numbers_converted)

Out put of the code above:

[8510725, 8510723, 8547169, 8510791, 8542798, 8542799]

You can use the built-in re module:

import re

with open('pgone.txt','r') as ifile:
    for line in re.findall('\d\n\d\n\d\n\d\n\d\n\d\n\d', ifile.read()):
        print(line.replace('\n', '')[::-1])

Output:

8510725
8510723
8547169
8510791
8542798
8542799

Explanation:

The pattern \d\n\d\n\d\n\d\n\d\n\d\n\d tells regex to return all substrings in file.read() that are 7 digits separated by a newline.

Related