Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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

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_Text - Fatal编程技术网

Python 两个文本文件中的行的组合

Python 两个文本文件中的行的组合,python,file,text,Python,File,Text,我有一个文本文件t1.txt: 1¶ 2¶ 3 »1¶ »2¶ »3 我有一个文本文件t2.txt: 1¶ 2¶ 3 »1¶ »2¶ »3 其中,>和分别表示制表符和换行符 我想将这两种方法结合起来,生成所有可能的组合: 11¶ 12¶ 13¶ 21¶ 22¶ 23¶ 31¶ 32¶ 33¶ 这是我的密码: out = 'out.txt' in1 = 't1.txt' in2 = 't2.txt' outFile = open(out,'w') with open(in1, 'r')

我有一个文本文件
t1.txt

1¶
2¶
3
»1¶
»2¶
»3
我有一个文本文件
t2.txt

1¶
2¶
3
»1¶
»2¶
»3
其中,
>
分别表示制表符和换行符

我想将这两种方法结合起来,生成所有可能的组合:

11¶
12¶
13¶
21¶
22¶
23¶
31¶
32¶
33¶
这是我的密码:

out = 'out.txt'
in1 = 't1.txt'
in2 = 't2.txt'
outFile = open(out,'w')
with open(in1, 'r') as f:
    for line1 in f:
        for line2 in open(in2, 'r'):
            outFile.write(line1+line2)
    outFile.close()
但我得到的结果是:

1¶
»1¶
1¶
»2¶
1¶
»32¶
»1¶
2¶
»2¶
2¶
»33»1¶
3»2¶
3»3
我不明白为什么。
有人能帮忙吗?

您想要:

因此,对于您的文件:

with open("a.txt") as f1, open("b.txt") as f2:
    print(list(product(*(map(str.rstrip,f1), map(str.rstrip,f2)))))
这将给你:

[('1', '1'), ('1', '2'), ('1', '3'), ('2', '1'), ('2', '2'), ('2', '3'), ('3', '1'), ('3', '2'), ('3', '3')]
并加入:

 print(list(map("".join, product(*(map(str.rstrip,f1), map(str.rstrip,f2))))))
['11', '12', '13', '21', '22', '23', '31', '32', '33']
要写入文件,请执行以下操作:

with open("a.txt") as f1, open("b.txt") as f2, open("out.txt", "w") as out:
    for p in product(*(map(str.rstrip,f1), map(str.rstrip, f2))):
        out.write("".join(p) + "\n")
输出:

11
12
13
21
22
23
31
32
33
对于python2,请使用
itertools.imap

product(*(imap(str.rstrip,f1),imap(str.rstrip, f2))

您的行包含换行符和空格。这些在行尾不可见

您必须清除这些字符:

out = 'out.txt'
in1 = 't1.txt'
in2 = 't2.txt'
with open(in2, 'r') as f:
    lines2 = [l.rstrip() for l in f]
with open(out,'w') as outFile:
    with open(in1, 'r') as f:
        for line1 in f:
            line1 = line1.rstrip()
            for line2 in lines2:
                outFile.write(line1+line2+'\n')

文件中有空格和返回。使用strip()修剪它们


你好查看此处:并搜索组合()。您知道缩进不正确吗?循环的第二个
嵌套在第一个循环中,这意味着它将用第二个循环的每一行打印第一个文件的每一行,然后用第二个循环的每一行打印第一个文件的第二行,等等@mic4ael是的,很抱歉在复制粘贴时发生了…@martineau为什么?它会用第二行打印第一行的每一行吗?嵌套循环不就是这样做的吗?我很困惑。如果我从两个大文本文件中获取输入,我该怎么做?我想合并行。。从这两个文件中…@418,我将进行编辑,但您只需在文件上应用相同的逻辑谢谢您的帮助!!:)非常感谢D这很快也很容易……)