首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我试图创建一个编辑文本的程序,让你从一些东西中选择,并将它们更改为几个选项中的一个

我试图创建一个编辑文本的程序,让你从一些东西中选择,并将它们更改为几个选项中的一个
EN

Stack Overflow用户
提问于 2020-03-14 00:26:03
回答 1查看 20关注 0票数 0
代码语言:javascript
复制
    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")

这就是我尝试过的,这是我的思考过程,一如既往,任何帮助都会被接受,请让一个笨蛋理解它:

EN

回答 1

Stack Overflow用户

发布于 2020-03-14 00:33:02

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

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

代码语言:javascript
复制
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")

但是你的问题很不清楚,我已经尽了最大的努力。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60674010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档