使用用户输入搜索并替换.txt文件中的字符串

时间:2018-12-18 22:03:34

标签: python string search replace user-input

背景:我正在尝试使工作流程的某些部分自动化。我采取的手动步骤:我得到一张更改文本文件中ID的票证。我必须浏览网络共享文件夹,创建文件的备份,将复制的文件推入存档文件夹,然后使用票证中的ID编辑现有文件。

我要创建的内容:一个小程序,将询问我要更改的ID(文本文件中已有ID),然后它将进入文件中并找到匹配项。然后,我要程序询问我要将ID更改为什么。我希望程序用我的输入来编辑现有文件,保存然后关闭。

到目前为止,我有以下代码完成了第一部分(复制文件并将其推送到存档文件夹中)。我有第二个功能,我一直坚持。我知道第二个功能不起作用,但是希望其他人提供他们认为我应该尝试的功能的意见。我已经看过fileinput模块,但是我已经读到它不是最好的模块,我应该尝试在没有fileinput模块的情况下对此进行编程。

代码

import shutil
import datetime
import os
import fileinput

original_file = "\\network\\path\\to\\file"

def date_rename_file():
    todays_date = datetime.datetime.now()
    stripped_time = todays_date.strftime('%Y%m%d')    
    shutil.copyfile(original_file, "\\network\\path\\to\\backupfolder\\device_"+str(stripped_time)+".txt")

def device_id_replace():
    original_id = input("What device ID are you needing to replace?")
    new_id = input("What is the new device ID?")
    with open(original_file, 'w') as devicetxt:
        for line in devicetxt:
            print(line)
            if original_id in line:
                print("Found "+original_id)

date_rename_file()
device_id_replace()

谢谢,请随时删除我的代码:)我仍在学习,希望您能提出任何建议。另外,如果我遗漏了任何相关信息,请随时告诉我!

3 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

def device_id_replace():
    original_id = input("What device ID are you needing to replace?")
    new_id = input("What is the new device ID?")
    with open(original_file, 'r+') as devicetxt: #r+ is for read and write
        string = devicetxt.read() #read into one string
        string = string.replace(original_id, new_id) #replacement
        devicetxt.truncate(0) #To clear contents of file before writeback
        devicetxt.seek(0) #Put the file's current position at 0
        devicetxt.write(string) #writeback to file

此外,更好的做法是将original_file作为字符串传递给函数。参见下面的编辑代码:

import shutil
import datetime
import os
import fileinput

original_file = "\\network\\path\\to\\file"

def date_rename_file(filepath):
    todays_date = datetime.datetime.now()
    stripped_time = todays_date.strftime('%Y%m%d')    
    shutil.copyfile(filepath, "\\network\\path\\to\\backupfolder\\device_"+str(stripped_time)+".txt")

def device_id_replace(filepath):
    original_id = input("What device ID are you needing to replace?")
    new_id = input("What is the new device ID?")
    with open(filepath, 'r+') as devicetxt: #r+ is for read and write
        string = devicetxt.read() #read into one string
        string = string.replace(original_id, new_id) #replacement
        devicetxt.truncate(0) #To clear contents of file before writeback
        devicetxt.seek(0) #Put the file's current position at 0
        devicetxt.write(string) #writeback to file

date_rename_file(original_file)
device_id_replace(original_file)

答案 1 :(得分:1)

尝试这个:

original_file = ""\\network\\path\\to\\file""

def device_id_replace(filepath):                   
    original_id = input("What device ID are you needing to replace?") 
    new_id = input("What is the new device ID?")   
    with open(filepath, 'r') as devicetxt:
        new_content = []
        for line in devicetxt:
            new_content.append(line.replace(original_id, new_id))
    with open(filepath, "w") as devicetxt:
        for line in new_content:
            devicetxt.write(line)

device_id_replace(original_file)

过去,@ ycx的答案无效。因此,我修复了您的旧代码:

original_file = "test.txt"


def device_id_replace(filepath):
    original_id = input("What device ID are you needing to replace?")
    new_id = input("What is the new device ID?")
    with open(filepath, 'r+') as devicetxt: #r+ is for read and write
        contents = devicetxt.readlines() #put all lines into a list
        for line_i, line in enumerate(contents):
            if original_id in line:
                contents[line_i] = line.replace(original_id, new_id) # CHANGED LINE
        devicetxt.truncate(0)
        devicetxt.seek(0)
        devicetxt.writelines(contents) #writeback to file

device_id_replace(original_file)

答案 2 :(得分:0)

您在这里有一些选择,或者使用sed之类的内部工具(如果您使用的是Unix / Unix之类),或者在旧文件的顶部创建一个新文件。

  1. 读取所有文件。
  2. 替换事件。
  3. 重写所有行。

这是pythonic版本

with open(filepath) as f:
    transformedLines = [line.replace(old, new) if (old in line) else (line) for line in f]
with open(filepath, 'w') as f:
    f.writelines(transformedLines);

较少的Pythonic版本类似:

transformedLines = []
with open(filepath) as f:
    for line in f:
        if old in line:
            transformedLines.append(line.replace(old, new))
        else:
            transformedLines.append(line)

可以使用map来实现一种实用的方法(尽管效率很低):

    transformedLines = list(map(lambda line : line.replace(old, new), f.readlines()))

如果文件不是很大,应该没问题,否则您将不得不考虑使用其他类型的数据存储,例如数据库。

您的方法目前效果很好,但是您希望以读取模式打开它,对于每行,请将其添加到新列表中(如果找到了ID),请添加新行。

将新数组写入在写入模式下打开的文件,这将清除其中的内容。

相关问题