Я пытаюсь создать программу, которая редактирует текст, позволяя вам выбрать из нескольких вещей и изменить их на один из нескольких вариантов - PullRequest
0 голосов
/ 13 марта 2020
    import os, re
config_file = "jsm_gyro_config.txt"
#fptr = open(config, "w")
#text = "demo text"
#fptr.write(text)
#fptr.close()

file = open(config_file, 'r')
file-read = file.read()

for line in file-read:
    if re.search(userinput, file-read):
        x = re.search(userinput, file-read)
        # iteminputted is what the user wants to replace
        iteminputted = "ref"
        startpostion = x.span[1] + 3
        endpostion = startposition + len(iteminputted)
        # Find out how to write to a specific location in a file that will finish this off
    else:
        print("Item not found")

Это то, что я пробовал, и вот мой мыслительный процесс, как всегда, любая помощь создается и, пожалуйста, сделайте ее понятной для идиота: (

1 Ответ

0 голосов
/ 13 марта 2020

Начнем с того, что вы не должны использовать - в объявлениях переменных, так как на самом деле это оператор и всегда будет рассматриваться как таковой. Он попытается вычесть.

Вот тот же код с исправленным, а также с входным значением

import os, re
config_file = "jsm_gyro_config.txt"
#fptr = open(config, "w")
#text = "demo text"
#fptr.write(text)
#fptr.close()

file = open(config_file, 'r')
file_read = file.read()
file.close() # You should always close your files.

for line in file_read:
    if re.search(userinput, file_read):
        x = re.search(userinput, file_read)
        # iteminputted is what the user wants to replace
        iteminputted = input("Input what you would like to replace > ")
        startpostion = x.span[1] + 3
        endpostion = startposition + len(iteminputted)
        # Find out how to write to a specific location in a file that will finish this off

    else:
        print("Item not found")

Однако ваш вопрос очень неясен, я сделал все, что мог.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...