我正在尝试创建一个程序,通过让您从一些内容中进行选择并将它们更改为几个选项之一来编辑文本

2024-04-24 00:56:52 发布

您现在位置:Python中文网/ 问答频道 /正文

    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条回答
网友
1楼 · 发布于 2024-04-24 00:56:52

首先,您不应该在变量声明中使用-,因为它实际上是一个运算符,并且将始终被视为运算符。它将尝试减去

下面是与固定和输入相同的代码

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")

不过你的问题很不清楚,我已经尽力了

相关问题 更多 >