Python - 从另一个文件写入新文件 [英] Python - Writing to a new file from another file

查看:112
本文介绍了Python - 从另一个文件写入新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望必须将File1中的第一个字段(Username)和第二个字段(Password)输出到在函数中创建的第三个文件中,但我无法执行此操作。 :(b / b)

这些文件的格式总是相同的,即:

文件1:用户名:DOB:名字:姓氏:::

File2:

 姓氏:密码



我的当前代码:

  def merge( f1,f2,f3):
以open(f3,a)作为outputFile:
以open(f1)作为usernameFile:
用于usernameFile中的行:
line = line [: - 3]
username = line.split(':')
outputFile.write(username [0])
with open(f2)as passwordFile:
for line password in password:
password = line.split(':')
outputFile.write(password [1])$ ​​b
$ b merge('file1.txt','file2。 txt','output.txt')

我想要File1的用户名和File2的密码使用布局写入File3:

 用户名:密码
用户名:密码
用户名:密码



任何帮助,将不胜感激。 :)

解决方案

这是您需要对代码进行工作所需的最小更改:

  def merge(f1,f2,f3):
with open(f3,a)as outputFile:

打开(f1)作为usernameFile:
作为usernameFile中的行:
username = line.split(':')[0]
lastname = line.split(': ')[3]
outputFile.write(用户名)
$ b $打开(f2)作为passwordFile:
用于passwordFile中的行:
lN,password =行。 split(':')
if lN == lastname:outputFile.write(password [1])$ ​​b
$ b merge('file1.txt','file2.txt','output。 txt')

然而,这种方法并不是很好,因为它多次读取一个文件。我会继续为第二个文件创建一个字典,并将姓氏作为关键字。字典在这些情况下非常有帮助。字典可以按如下方式创建:

  def makeDict(f2):
dOut = {}
打开(f2)作为f:
for l in f:
dOut [l.split(':')[0]] = l.split(':')[1]

return dOut


def merge(f1,f2,f3):

pwd = makeDict(f2)
print pwd
with open(f3,a)as outputFile:

with open(f1)as usernameFile:
用于usernameFile中的行:
if line.strip( )=='':continue
username = line.split(':')[0]
lastname = line.split(':')[3]
如果pwd中的姓氏:
outputFile.write(username +':'+ pwd [lastname] +'\\\
')


merge('f1.txt','f2.txt' ,'f3.txt')

我使用这些文件运行以下程序:



f1.txt

 用户名0:DOB:名字:姓氏0 ::: 
用户名1:DOB:F Irstname:Lastname1 :::
Username2:DOB:Firstname:Lastname2 :::
Username3:DOB:Firstname:Lastname3 :::



f2.txt

 姓氏0:密码0 
姓氏1:密码1
Lastname2:Password2
Lastname3:Password3

得到了输出:



 用户名0:密码0 

用户名1:密码1

用户名2:密码2

用户名3:密码3

我添加了最后一行合并(...)以及另一个用于跳过输入文本中的空白行,但除此之外,一切都应该没问题。如果合并(... 函数未被调用,那么不会有任何输出。


I wish to have to have the first field (Username) from File1 and the second field(Password) output into a third file which is created during the function but I am unable to do it. :(

The format of the files will always be the same which are:

File 1:

Username:DOB:Firstname:Lastname:::

File2:

Lastname:Password

My current code:

def merge(f1,f2,f3):
   with open(f3, "a") as outputFile:
      with open(f1) as usernameFile:
         for line in usernameFile:
            line = line[:-3]
            username = line.split(':')
            outputFile.write(username[0])
      with open(f2) as passwordFile:
         for line in passwordFile:
            password = line.split(':')
            outputFile.write(password[1])

merge('file1.txt', 'file2.txt', 'output.txt')

I want the Username from File1 and the Password from File2 to write to File3 with the layout:

Username:Password
Username:Password
Username:Password

Any help would be appreciated. :)

解决方案

This is the minimum change that you would need to do to your code to make it work:

def merge(f1,f2,f3):
  with open(f3, "a") as outputFile:

     with open(f1) as usernameFile:
        for line in usernameFile:
           username = line.split(':')[0]
           lastname = line.split(':')[3]
           outputFile.write(username)

        with open(f2) as passwordFile: 
           for line in passwordFile:
              lN, password = line.split(':')
              if lN == lastname: outputFile.write(password[1]) 

merge('file1.txt', 'file2.txt', 'output.txt')

However, this method isn't very good because it reads a file multiple times. I would go ahead and make a dictionary for the second file, with the lastname as a key. Dictionaries are very helpful in these situations. The dictionary can be made apriori as follows:

def makeDict(f2):
  dOut = {}
  with open(f2) as f:
     for l in f:
        dOut[ l.split(':')[0] ] = l.split(':')[1]

  return dOut


def merge(f1,f2,f3):

  pwd = makeDict(f2)
  print pwd
  with open(f3, "a") as outputFile:

     with open(f1) as usernameFile:
        for line in usernameFile:
           if line.strip() == '': continue
           username = line.split(':')[0]
           lastname = line.split(':')[3]
           if lastname in pwd: 
              outputFile.write(username + ':' + pwd[lastname] + '\n')


merge('f1.txt', 'f2.txt', 'f3.txt'  )

I just ran the following program using the files:

f1.txt

Username0:DOB:Firstname:Lastname0:::
Username1:DOB:Firstname:Lastname1:::
Username2:DOB:Firstname:Lastname2:::
Username3:DOB:Firstname:Lastname3:::

f2.txt

Lastname0:Password0
Lastname1:Password1
Lastname2:Password2
Lastname3:Password3

and got the output:

Username0:Password0

Username1:Password1

Username2:Password2

Username3:Password3

I did add the last line merge(...) and another like which would be used to skip blank lines in the input text, but otherwise, everything should be fine. There wont be any output if the merge(... function isn't called.

这篇关于Python - 从另一个文件写入新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆