python-3.x 如何在文件中找到一个单词或行,并将其下的行替换为新单词?

shstlldc  于 2023-05-30  发布在  Python
关注(0)|答案(2)|浏览(96)

我很抱歉,如果这似乎noobish,但我不能找到我到底想做什么。我做了一个投标工作程序,我想使用一个文本文件作为配置文件来存储定价等程序可以根据需要读写。这是一个基本的我所拥有的。
验证码:

def job():
    question1 = input("Do you want to adjust your pricing? ").lower()
    if question1 == "y" or question1 == "yes":
        question2 = input("\nWhat price point would you like to change?\nFlooring cost(1), Basic repair cost(2), or Fuel mileage cost(3): ")
        if question2 == "1":
            flooring_cost = input("\nWhat is your charge per sqft for flooring? ")
            with open("JobFiles.txt", "r") as fo, open("JobFiles.txt", "a") as fw:
                for line in fo:
                    if line == "Floor Price":
                        next(fo)
                        fw.write(flooring_cost + "\n")
                fo.close()
                fw.close()
            return job()

::Example JobFiles.txt::

Flooring Price
2.50    <----------<<
Basic Repair Price
25
Fuel Charge
0.40

::目标文件示例::

Flooring Price
2.75    <----------<<
Basic Repair Price
25
Fuel Charge
0.40

因此,它应该读取文件“JobFiles.txt”并将“地板价格”下的行替换为用户输入。我可以让它什么也不做或擦除文件,但不是我的意图。
编辑:它应该搜索文本文件中的一个词,即“地板价格”,并取代它下面的行,这样文件就可以增长和改变,而不必重新编码,以调整“地板价格”是在第1行还是第100行。

wwtsj6pe

wwtsj6pe1#

你可以这样做。

def job(value):
    question1 = input("Do you want to adjust your pricing? ")
    question1 = question1.lower()
    if question1[0] == "y" or question1 == "yes":
        question2 = input("\nWhat price point would you like to change?\nFlooring cost(1),"
                          "Basic repair cost(2), or Fuel mileage cost(3): ")
        if question2 == "1":
            flooring_cost = input("\nWhat is your charge per sqft for flooring? ")
            value[0] = flooring_cost
    return value

filename = "JobFiles.txt"
f = open(filename, 'r')
key = []
value = []
while True:
    line1 = f.readline().rstrip() # reading odd no. line
    line2 = f.readline().rstrip() # reading even no. line
    if not line2:
        break
    else:
        key.append(line1) # every odd no. line is the key, ex. Flooring Price
        value.append(line2) # every even no. line is the value, ex. 2.50
f.close()

value = job(value)
fo = open(filename, 'w')
for key, value in zip(key, value):
    fo.write(key + '\n' + value + '\n') # writing pairs of line in file
fo.close()
crcmnpdw

crcmnpdw2#

经过近一个星期和3357行代码写我已经完成了我的程序!下面是我想弄明白的方法,在文件中搜索一行,然后替换它下面的一行。
::查找信息::

with open("test_folder/test", "r") as fo:#<---<< opens the file in read mode
    data = fo.readlines()#<---<< saves the file to a list variable
for i, line enumerate(data):#<---<< gets the line numbers
    if "key word" in line:#<---<< searches for key word
        n = i + 1#<---<< takes the line number from the key word and adds one aka moves down one line
        var = data[n].strip()#<---<< adds line from file to a variable and removes the /n

::用于更改文件::

with open("test_folder/test", "r") as fo:  #<---<<
    data = fo.readlines()                   #<---<<
for i, line enumerate(data):                #<---<<
    if "key word" in line:                  #<---<<
        n = i + 1                           #<---<<
data[n] = "%s\n" % (var2)#<---<< changes the value on the line in the list
with open("test_folder/test", "w") as fw:#<---<< opens file in write mode
    fw.writelines(data)<---<< writes the altered list back to file

我希望这是明确的,可以帮助别人。总有更好的方法来做事情,所以无论如何,如果你有任何请纠正我或添加。

相关问题