Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
23
Indexable
Never
fileLines = []

def findCommaNearMiddle(line):
    length = len(line)
    middle = int(length/2)
    # check values on either side until comma is found
    distance = 0
    while distance < middle - 1:
        if line[middle+distance] == '、' or line[middle+distance] == '。':
            return middle+distance
        elif line[middle-distance] == '、' or line[middle+distance] == '。':
            return middle-distance
        distance += 1
    return -1 # idealy, this will never happen

with open("test.txt", "r", encoding="utf8") as file:
    fileText = file.read()
    fileLines = fileText.split('\n');
    for i in range(len(fileLines)):
        line = fileLines[i]
        size = len(line)
        if size > 45:
            middleComma = findCommaNearMiddle(line)    
            if middleComma == -1:
                continue
            fileLines[i] = line[:middleComma]
            fileLines.insert(i+1, line[middleComma+1:]) # +1 to get rid of comma
    file.close()

with open("test.txt", "w", encoding="utf8") as file:
    for line in fileLines:
        file.write(line + '\n')

    file.close()