Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—从一个文件写入另一个文件(文件排序不一致)_Python_File_Function_Dictionary_Sorted - Fatal编程技术网

Python—从一个文件写入另一个文件(文件排序不一致)

Python—从一个文件写入另一个文件(文件排序不一致),python,file,function,dictionary,sorted,Python,File,Function,Dictionary,Sorted,下面的代码允许我通过函数传递文本文件,然后输出新创建的第三个文件,其中包含用户名和密码,如下所示: 文件1格式(用户名) 文件2格式(密码) 文件3格式(新创建的输出文件) 当前代码工作正常,直到输入文件排序不一致,导致输出文件结果不对齐且不正确。我基本上希望将输入文件中的用户名和密码输入到一个新文件中,并且在没有正确排序时不会出错 from itertools import izip def merge(f1,f2,f3): with open(f3, "a") as outputF

下面的代码允许我通过函数传递文本文件,然后输出新创建的第三个文件,其中包含用户名和密码,如下所示:

文件1格式(用户名)

文件2格式(密码)

文件3格式(新创建的输出文件)

当前代码工作正常,直到输入文件排序不一致,导致输出文件结果不对齐且不正确。我基本上希望将输入文件中的用户名和密码输入到一个新文件中,并且在没有正确排序时不会出错

from itertools import izip

def merge(f1,f2,f3):

   with open(f3, "a") as outputFile:

      for line_from_f1, line_from_f2 in izip(open(f1), open(f2)):

         username = line_from_f1.split(':')[0]
         password = line_from_f1.split(':')[1]
         outputfile.write("%s:%s" % (username, password))

merge("input1.txt", "input2,txt", "output.txt")
我认为字典是一条路要走,但到目前为止,我的尝试没有成功,并且出现了各种关键错误。任何帮助都将不胜感激。:)

最佳但会产生关键错误:

def merge(f1,f2,f3):

   d1 = {}
   with open(f1, 'r') as f:
      for line in f:
         d1[line.split(':')[3]] = line.split(':')[0]

   d2 = {}
   with open(f2, 'r') as f:
      for line in f:
         d2[line.split(':')[0]] = line.split(':')[1]

   with open(f3, 'w') as f:
      for key in d1:
         f.write("{}:{}\n".format(d1[key], d2[key]))

您的最佳尝试在哪里,以及失败的确切程度(提供和完整的错误回溯)?谢谢-当所有用户名和密码配对时,它会工作,但是如果一个用户名无法破解,它会将该用户名与下一个被破解的密码匹配,并且不会删除没有密码的用户名,因为它没有被成功破解。不-当所有密码都被破解时效果很好,但是当我用一个不会被破解的伪造哈希将一个测试帐户添加到第一个文件中时,该函数将其与下一个破解的密码配对。仅当第二个文件中存在input1.txt文件的哈希时,它才会输出某些内容。我可以向您保证,它永远不会将用户名与错误的密码配对。我现在可以使用它了。我得到了
ValueError:列表1中用户名、id、名称、哈希、空格的
值太多,无法解压缩错误:S
Username:Password
from itertools import izip

def merge(f1,f2,f3):

   with open(f3, "a") as outputFile:

      for line_from_f1, line_from_f2 in izip(open(f1), open(f2)):

         username = line_from_f1.split(':')[0]
         password = line_from_f1.split(':')[1]
         outputfile.write("%s:%s" % (username, password))

merge("input1.txt", "input2,txt", "output.txt")
def merge(f1,f2,f3):

   d1 = {}
   with open(f1, 'r') as f:
      for line in f:
         d1[line.split(':')[3]] = line.split(':')[0]

   d2 = {}
   with open(f2, 'r') as f:
      for line in f:
         d2[line.split(':')[0]] = line.split(':')[1]

   with open(f3, 'w') as f:
      for key in d1:
         f.write("{}:{}\n".format(d1[key], d2[key]))
dict1 = {}
list1 = []
fs = ":"
with open("input1.txt") as f:
    for line in f:
       list1.append(line.rstrip('\n').split(fs))

with open("input2.txt") as f:
    for line in f:
      (key, val) = line.rstrip('\n').split(fs)
      dict1[key] = val   

with open("output.txt","w") as f:
    for username, id, name, hash, b1, b2, b3 in list1:
        if hash in dict1:
            f.write("%s:%s\n"%(username,dict1[hash]))