【问题标题】:perform multiple write operations on a text file in python在python中对文本文件执行多个写操作
【发布时间】:2020-11-16 15:49:16
【问题描述】:

我正在尝试将文本文件清理为所需的格式。为了实现这一点,我目前正在打开文件,执行某个操作并关闭它。我对其他一些操作重复相同的操作。有没有更好的方法来做到这一点。我要执行的操作是

  1. 打开文本文件,用新字符串替换某个字符串
  2. 删除上面所有的字符串直到新的字符串
  3. 删除文本文件中的所有选项卡
  4. 用逗号替换所有空格

此外,我每次都将输出写入一个新文件。我可以在执行格式化操作的同时在同一个文件中执行所有这些操作吗?

文本文件示例

ANX ANV 91 BB
cc yy the 12
okr out 1 11 


temp1   temp2(a/b)      temp3(t)  temp4(x)
0   11  9a   1.1
1   22  9b   12
2   33  9c   4
3   44  9d   92

预期:

temp1,temp2,temp3,temp4
0,11,9a,1.1
1,22,9b,12
2,33,9c,4
3,44,9d,92

我的代码

OldString = "temp1   temp2(a/b)      temp3(t)  temp4(x)"
NewString = "temp1 temp2 temp3 temp4"


def myfunction():    
    inputFile = open("Temp1.txt",encoding="utf8")
    exportFile = open('File1.txt','w',encoding="utf8")

    with inputFile as f:
        #Repalce old string by NewString
        newText=f.read().replace(OldString,NewString)
    
    with exportFile as f:
        f.write(newText)
        
    lines_to_write = []
    tag_found = False
 
    #Delete all extra strings upto the NewString.
    #Also remove any tabs
    with open('File1.txt',encoding="utf8") as in_file:
        for line in in_file:
            if line.strip() == NewString:
                tag_found = True
            if tag_found:
                lines_to_write.append(line.replace('\t', ' '))

    with open('File2.txt','w',encoding="utf8") as out_file:
        out_file.writelines(lines_to_write)

    lines_to_write = []
    tag_found = False

    #Replace all blank spaces with comma
    with open('File2.txt',encoding="utf8") as in_file:
        for line in in_file:
            if line.strip() == NewString:
                tag_found = True
            if tag_found:
                lines_to_write.append(line.replace(' ', ','))
        
        with open('File3.txt','w',encoding="utf8") as out_file:
            out_file.writelines(lines_to_write)

谢谢

【问题讨论】:

  • 你能解释一下是什么问题吗?我们不应该为您做这件事,只是帮助您找到错误或其他东西......
  • 嗨@adir。感谢您的关注。正如我的问题中提到的,我正在尝试改进代码。并且还找到了如何一次执行多个操作,而无需一次又一次地打开和关闭文件。

标签: python text formatting


【解决方案1】:

我写了一个小函数来做你想做的事。它首先查看要附加到新文件中的第一行。然后它格式化并附加到一个数组中,只有在这个之后的行。然后它创建一个写入这个新数组的文件。

# Get index of first string to insert
def get_index(file, string):
    for line in file:
        if string in line:
            return file.index(line)


# Main function to write files
def write_files(in_file, out_file, old_string, new_string):

    # Opens input file, loads to a list then close
    with open(in_file, 'r', encoding="utf8") as input_file:
        old_file = input_file.readlines()

    # Discover index of old string you want to replace
    new_string_index = get_index(old_file, old_string)

    # Crates array with your new string formatted
    lines_to_write = [','.join(new_string.split())]

    # Populate lines_to_write array with lines formatted from the index you want
    for old_line in old_file[new_string_index + 1:]:
        new_line = old_line.strip().split()
        lines_to_write.append(','.join(new_line))

    # Write lines_to_write array to output file
    with open(out_file, 'w', encoding="utf8") as export_file:
        for line in lines_to_write:
            export_file.write(line)
            export_file.write('\n')


# Call function with your variables
if __name__ == "__main__":
    OldString = "temp1   temp2(a/b)      temp3(t)  temp4(x)"
    NewString = "temp1 temp2 temp3 temp4"
    inputFile = "Temp1.txt"
    exportFile = 'File1.txt'
    write_files(inputFile, exportFile, OldString, NewString)

【讨论】:

  • 感谢您的回复。我试过你的代码。它完成了大部分格式化。但它也在字符串中添加了额外的逗号。 t,e,m,p,1, ,t,e,m,p,2, ,t,e,m,p,3, ,t,e,m,p,4。我会努力解决的。
  • 对不起,我忘记在加入之前拆分新字符串。应该是 lines_to_write = [','.join(new_string.split())] 我现在已经编辑了。
猜你喜欢
  • 2021-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
相关资源
最近更新 更多