-2

good day everyone, so I have this problem, I have this script that counts the number of delimiters in the lines and then adds them to a new file / but I don't know how to do it, they need to remove the delimiter at the end

for example if I have text input = "1:2:3 ThisIs,Test,"

the numbers present 1 book 2 chapter 3 verse and I want to add quarters and that is the number of characters (without the last character at the end of the text in line)

count delimiter add count two / I want them to be ignored at the end

with open('123.txt', encoding='utf-8') as input_file:
    texts = input_file.readlines()

delimiters = ',.'

for text in texts:
    count = 0
    # We count the number of characters from the list of delimiters in the given line
    for character in text:
        if character in delimiters:
            count += 1

    # We add the number of characters from the delimiters list to the end of the line numbers
    text_parts = text.split(' ')
    text_parts[0] = text_parts[0] + ':' + str(count)
    new_text = ' '.join(text_parts)
    print(new_text, file=open(""+str("10000")+".txt", "a", encoding='utf-8'))

basically, what should I do to add the code after three numbers, the fourth number, for example, I have input

input = "1:2:3 ThisIs,Test," im search output = "1:2:3:1 ThisIs,Test," [,] this last in text ignored

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
ZION BEAST
  • 21
  • 1
  • 1
    Welcome to Stack Overflow! Could you please elaborate on what is `1:2:3` here and how does it become `1:2:3:1`? Also, please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to make it easier for fellow developers to provide assistance! – The Myth Dec 15 '22 at 17:00
  • @TheMyth that is the number / book chapter verse and the third is how many characters there is in total I want to add the last one but without the last character at the end of the text and I can't do it, they need help with that – ZION BEAST Dec 15 '22 at 17:02
  • Also, please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)" – The Myth Dec 15 '22 at 17:03
  • It would be a hassle in a whole lot of other languages, but in Python in particular, you can write `"asdf"[:-1]` to throw away a character from the end. And similarly, `"asdf"[-1]` will get you the last character, so you can check what it is, even if `"asdf".endswith("f")` is not familiar. – tevemadar Dec 15 '22 at 17:04
  • @TheMyth I edited the question and added a description of the numbers (and the fact that the fourth number is the number of characters I want to count in the line) – ZION BEAST Dec 15 '22 at 17:06
  • I have no idea what you're trying to do. Could you show a short example of your file (maybe four or five lines) along with *exactly* what you want the output to be? – CrazyChucky Dec 15 '22 at 18:11
  • @CrazyChucky I already solved it before you wrote – ZION BEAST Dec 15 '22 at 18:49
  • Glad it worked for you. Please still consider clarifying your question, as well as adding some explanation to your (currently code-only) answer. This site is a resource for others as well. – CrazyChucky Dec 15 '22 at 18:52
  • Does this answer your question? [How do I remove a substring from the end of a string?](https://stackoverflow.com/questions/1038824/how-do-i-remove-a-substring-from-the-end-of-a-string) – tevemadar Jan 15 '23 at 14:09

1 Answers1

0

solved

with open('123.txt', encoding='utf-8') as input_file:
    # Načteme obsah souboru do proměnné texts
    texts = input_file.readlines()


import re
delimiters = ',.'
for text in texts:
    text = text.strip()
    count = 0
    for character in text.replace('\n', ''):
        if character in delimiters: 
            count += 1
    if text.endswith((".",",")) == True: 
        count -= 1
    text_parts = text.split(' ')
    text_parts[0] = text_parts[0] + ':' + str(count)
    new_text = ' '.join(text_parts)
    print(new_text, file=open(""+str("10000")+".txt", "a", encoding='utf-8'))
ZION BEAST
  • 21
  • 1