Python:用多个输入替换文本文件中的多个字符串 [英] Python: Replace multiple strings in text file with multiple inputs

查看:86
本文介绍了Python:用多个输入替换文本文件中的多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用用户输入替换多个字符串.以下代码(是对stackoverflow中其中一个查询的代码的修改;对不起,我再也找不到该线程)在查找和替换指定字符串的一个实例(故意完成)时效果很好./p>

I am looking at replacing multiple strings with input from the user. The following code (which is a modification of a code from one of the queries here in stackoverflow; pardon coz I can't find the thread anymore) works well when finding and replacing one instance(done on purpose) of a specified string:

print('What word should we replace s1 with?')
input_1 = input()
with open('C:\\dummy1.txt')as f:
     sample1 = f.read().replace("s1", str(input_1), 1)
with open('C:\\dummy2.txt',"w") as f1:
     f1.write(sample1)

现在,当我尝试将其复制粘贴并修改以适应其他字符串时,只有最后指定的字符串会被替换...这是示例代码:

Now when I try copy-pasting the same and modifying it to cater to other strings, only the last specified string gets replaced... here's a sample code:

print('What word should we replace s1 with?')
input_1 = input()
with open('C:\\dummy1.txt')as f:
     sample1 = f.read().replace("s1", str(input_1), 1)
with open('C:\\dummy2.txt',"w") as f1:
     f1.write(sample1)

print('What word should we replace s2 with?')
input_2 = input()
with open('C:\\dummy1.txt')as f:
     sample2 = f.read().replace("s2", str(input_2), 1)
with open('C:\\dummy2.txt',"w") as f1:
     f1.write(sample2)

我需要怎么做才能无缝处理多个字符串?请考虑向经验不足一年的编码人员以及使用python进行9个小时的视频学习的人讲解它:)谢谢!

What do I need to do to make this work seamlessly for multiple strings? Please consider explaining it to someone with less than a year experience in coding and 9-hour worth of video learning in python :) Thank you!

推荐答案

这对我有用.

#using os to get path since py and txt file are in same folder
#just change your actual path if you need
import os
curdir=os.getcwd()
filepath=os.path.join(curdir,'the_file.txt')


print('What word should we replace s1 with?')
input_1 = input()
print('What word should we replace s2 with?')
input_2 = input()

sample1=''
sample2=''
with open(filepath, 'r')as f:
     sample1 = f.read().replace("s1", str(input_1), 1)
     sample2 = sample1.replace("s2", str(input_2), 1)
with open(filepath, 'w')as f:    
     f.write(sample2)

我还刚刚意识到,在您获得输入并替换文本的同时,您都从dummy1.txt中读取并写入dummy2.txt中.这就是仅更改s2的原因.更改s2时,您应该从dummy2.txt中读取内容,因为这是包含s1更改的文件.在上面的示例中,我只是覆盖了我读取的文件,但是如果需要,可以轻松更改.

Also I just realized than you read from dummy1.txt and write to dummy2.txt both times that you get inputs and replace text. That's the reason that only s2 is being changed. You should read from dummy2.txt when changing s2 because that is the file that contains the s1 change. In my example above I just overwrite the file I read from, but that's easily changed if you want.

    #using os to get path since py and txt file are in same folder
#just change your actual path if you need
import os
curdir=os.getcwd()
filepath=os.path.join(curdir,'the_file.txt')
filepath_2=os.path.join(curdir,'the_other_file.txt')


print('What word should we replace s1 with?')
input_1 = input()
print('What word should we replace s2 with?')
input_2 = input()

sample1=''
sample2=''
with open(filepath, 'r')as f:
     sample1 = f.read().replace("s1", str(input_1), 1)
     sample2 = sample1.replace("s2", str(input_2), 1)
with open(filepath_2, 'w')as f:    
     f.write(sample2)

这篇关于Python:用多个输入替换文本文件中的多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆