用在文件1中找到的数据更新文件2中的记录

2024-04-25 03:31:53 发布

您现在位置:Python中文网/ 问答频道 /正文

有一个固定格式的大文件file1。另一个CSV文件file2具有id和值,使用这些id和值,需要更新file1中具有相同id的记录的特定部分。这是我的尝试。我真的很感激你能为这项工作提供的任何帮助。你知道吗

文件2逗号分隔

clr,code,type
Red,1001,1
Red,2001,2
Red,3001,3
blu,1002,1
blu,2002,2
blu,3002,3

文件1(固定宽度格式)

clrtyp1typ2typ3notes
red110121013101helloworld
blu110221023102helloworld2

file1需要更新为以下内容

clrtyp1typ2typ3notes
red100120013001helloworld
blu100220023002helloworld2

请注意,这两个文件都是相当大的文件(每个文件有多GB)。我是白痴,请原谅我犯的任何严重错误。我非常感谢你能提供的任何帮助。你知道吗

import shutil
#read both input files
file1=open("file1.txt",'r').read()
file2='file2.txt'

#make a copy of the input file to make edits to it. 
file2Edit=file2+'.EDIT'
shutil.copy(file2, baseEdit)
baseEditFile = open(baseEdit,'w').read()

#go thru eachline, pick clr from file1 and look for it in file2, if found, form a string to be replaced and replace the original line. 
with open('file2.txt','w') as f:
    for line in f:
        base_clr = line[:3]
        findindex = file1.find(base_recid)
        if findindex != -1:
            for line2 in file1:
                #print(line)
                clr = line2.split(",")[0]
                code = line2.split(",")[1]
                type = line2.split(",")[2]
                if keytype = 1:
                    finalline=line[:15]+string.rjust(keyid, 15)+line[30:]
                    baseEditFile.write( replace(line,finalline)
                    baseEditFile.replace(line,finalline)

Tags: 文件totxtidforreadlinered
1条回答
网友
1楼 · 发布于 2024-04-25 03:31:53

如果我说对了,你需要这样的东西:

# declare file names and necessary lists
file1 = "file1.txt"
file2 = "file2.txt"
file1_new = "file1.txt.EDIT"
clrs = {}

# read clrs to update
with open(file1, "r") as f:
    # skip header line
    f.next()
    for line in f:
        clrs[line[:3]] = []

# read the new codes
with open(file2, "r") as f:
    # skip header
    f.next()
    for line in f:
        current = line.strip().split(",")
        key = current[0].lower()
        if key in clrs:
            clrs[key].append(current[1])

# write the new lines (old codes replaced with the new ones) to new file
with open(file1, "r") as f_in:
    with open(file1_new, "w") as f_out:
        # writes header
        f_out.write(f_in.next())
        for line in f_in:
            line_new = list(line)
            key = line[:3]
            # checks if new codes were found for that key
            if key in clrs.keys():
                # replaces old keys by the new keys
                line_new[3:15] = "".join(clrs[key])
            f_out.write("".join(line_new))

这只对给定的示例有效。如果您的文件有另一种格式在实际使用中,您必须调整所使用的索引。你知道吗

这个小脚本首先打开file1,遍历它,并将clr作为键添加到字典中。该键的值是一个空列表。 然后它打开file2,并遍历这里的每个clr。如果clr在字典中,它会将代码附加到列表中。因此,在运行这部分之后,字典包含键、值对,其中键是clr,值是包含代码的列表(按照文件给出的顺序)。你知道吗

在脚本的最后一部分,file1.txt的每一行都会写入file1。txt.EDIT文件. 在编写之前,旧的代码将被新的代码替换。你知道吗

保存在file2.txt中的代码必须与保存在file1.txt中的代码顺序相同。如果顺序可能不同,或者file2.txt中的代码可能多于file1.txt中需要替换的代码,则需要添加一个查询来检查正确的代码。这没什么大不了的,但是这个脚本可以解决你的问题,比如你给我们的文件。你知道吗

如果您有任何问题或需要更多帮助,请随时提出。你知道吗

编辑:除了你在问题代码中犯的一些语法错误和错误的方法调用之外,你不应该一次读入保存在文件中的全部数据,尤其是如果你知道文件可能会变得非常大的话。这会消耗大量内存,并可能导致程序运行非常慢。这就是为什么逐行迭代更好。我提供的示例一次只读取文件的一行,并将其直接写入新文件,而不是将旧文件和新文件都保存在内存中并作为最后一步写入。你知道吗

相关问题 更多 >