简体   繁体   English

写入文本文件每一行中的两个特定位置(或者创建一个添加了信息的新文件)

[英]Writing to two specific positions in each line of a text file (or rather creating a new file with the information added)

aspiring Python newb (2 months) here. 这里有抱负的Python newb(2个月)。 I am trying to create a program that inserts information to two specific places of each line of a .txt file, actually creating a new file in the process. 我正在尝试创建一个将信息插入.txt文件每一行的两个特定位置的程序,实际上是在此过程中创建了一个新文件。

The information in the source file is something like this: 源文件中的信息是这样的:

1,340.959,859.210,0.0010,VV53
18abc,34099.9590,85989.2100,0.0010,VV53
00y46646464,34.10,859487.2970,11.4210,RP27

Output would be: 输出为:

1,7340.959,65859.210,0.0010,VV53
18abc,734099.9590,6585989.2100,0.0010,VV53
00y46646464,734.10,65859487.2970,11.4210,RP27

Each line different, hundreds of lines. 每行不同,几百行。 The specific markers I'm looking for are the first and second occurence of a comma (,). 我要查找的特定标记是逗号(,)的第一次和第二次出现。 The stuff needs to be added after the first and second comma. 这些内容需要在第一个和第二个逗号之后添加。 You'll know what I mean when you see the code. 看到代码,您就会明白我的意思。

I have gotten as far as this: the program finds the correct places and inserts what I need, but doesn't write more than 1 line to the new file. 到目前为止,我已经找到了:该程序找到正确的位置并插入我需要的内容,但不会在新文件中写入多于1行的内容。 I tried debugging and seeing what's going on 'under the hood', all seemed good there. 我尝试调试,看看“幕后”发生了什么,一切似乎都很好。

Lots of scrapping code and chin-holding later I'm still stuck where I was a week ago. 后来我进行了很多剪贴代码和下巴操作,仍然停留在一周前的状态。

tl;dr Code only outputs 1 line to new file, need hundreds. tl; dr代码仅将1行输出到新文件,需要数百行。

f = open('test.txt', 'r')
new = open('new.txt', 'w')

first = ['7']
second = ['65']
line = f.readline()
templist = list(line)
counter = 0

while line != '':
    for i, j in enumerate(templist):
        if j == ',':
            place = i + 1
            templist1 = templist[:place]
            templist2 = templist[place:]
            counter += 1    
            if counter == 1:
                for i, j in enumerate(templist2):
                    if j == ',':
                        place = i + 1
                        templist3 = templist2[:place]
                        templist4 = templist2[place:]
                        templist5 = templist1 + first + templist3 + second + templist4
                        templist6 = ''.join(templist5)
                        new.write(templist6)
                        counter += 1
                        break                     
                if counter == 2:
                    break
            break

    line = f.readline()
    templist = list(line)

f.close()
new.close()

If I'm understanding your samples and code correctly, this might be a valid approach: 如果我正确理解了您的示例和代码,这可能是一种有效的方法:

with open('test.txt', 'r') as infd, open('new.txt', 'w') as outfd:
    for line in infd:
        fields = line.split(',')
        fields[1] = '7' + fields[1]
        fields[2] = '65' + fields[2]
        outfd.write('{}\n'.format(','.join(fields)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM