如何使用python从记事本文件中提取数据并放入csv中的特定行?

2024-04-26 00:50:13 发布

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

我有一个记事本文件和一个csv文件。我想从记事本文件中提取“new\u password”数据,并使用python将其粘贴到csv文件的password列中。请参考附件并帮助我使用python代码。我成功地将整个文本文件内容写入csv文件的密码列中,但我只想将新的密码数据写入csv文件。你知道吗

import csv

f1 = open ("format.txt","r") # open input file for reading
for i in f1.readlines()[1:]:
        if ';' in i:
            temp=i.split(';')
            f1 = temp[1]

with open('out.csv', 'wb') as f: # output csv file
    writer = csv.writer(f)
    with open('passwords_sample.csv','r') as csvfile: # input csv file
        reader = csv.reader(csvfile, delimiter=',')
        for row in reader:  
            row[4] = f1.readlines() # edit the 8th column 
            writer.writerow(row)
f1.close()

Tags: 文件csv数据in密码forinputpassword