'change the data in the file change numbers

I have a problem and I need to solve it, specifically reduce all the numbers example I have a text file input from file

1/1/1 text data
20/20/20 text data 1
300/300/300 text data 2 
10/10/10 text data

this script should do it

def digit_root (n):
     if n == 0: return 0
     return (n - 1)% 9 + 1
print (digit_root (20))

and the output should be

1/1/1 text data
2/2/2 text date 1
3/3/3 text date 2
1/1/1 text data 3

file =  open("dadd.txt",encoding='ISO-8859-1')
lines = file.readlines()
for line in lines:
    words = line.split()
    print(words[0])

this will only give numbers the question is how used digit_root to those numbers

This script should clean each row sequentially and what they need to do is change the digits on each row from file



Solution 1:[1]

Suppose "in.txt" is

1/1/1 text data
20/20/20 text data 1
300/300/300 text data 2
10/10/10 text data 3
250/250/250 text data 4

The following program opens two files, "in.txt" and "out.txt". The former file is opened for reading and the latter for writing. As we iterate over the lines of f_in, we create a temporary variable temp that is initially assigned the return value of line.split('/') which is a list of the "words" in the line, using "/" as the delimiter string. We then split the last element of temp (i.e. element temp[-1]) into a list of two "words", using the first occurrence of consecutive whitespace as the delimiter and then assign this to temp[-1:], effectively extending temp by one element. We then use an f-string to help us write each line to "out.txt" in our desired format.

The argument maxsplit = 2 in the first split() call is to ensure that temp initially has only three elements (before we extend it by one). If we did not specify anything for maxsplit, the text to the right of the numbers may have "/" somewhere which would make temp have more than three elements initially, which would make the below code fail. Similarly, maxsplit = 1 is used in the second split() call along with the first argument equal to None to split the string at the first occurrence of consecutive whitespace.

def digit_root(n):
    return (n - 1) % 9 + 1 if n else 0

with open("in.txt") as f_in, open("out.txt", "w") as f_out:
    for line in f_in:
        temp = line.split('/', maxsplit = 2)
        temp[-1:] = temp[-1].split(None, maxsplit = 1)

        # formatted string
        out_line = f"{digit_root(int(temp[0]))}/{digit_root(int(temp[1]))}/" \
                   f"{digit_root(int(temp[2]))} {temp[3]}"
        print(out_line, end = "")

        # write out_line to "out.txt"
        f_out.write(out_line)

Output (from print)

1/1/1 text data
2/2/2 text data 1
3/3/3 text data 2
1/1/1 text data 3
7/7/7 text data 4

"out.txt" (after running above program)

1/1/1 text data
2/2/2 text data 1
3/3/3 text data 2
1/1/1 text data 3
7/7/7 text data 4

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1