简体   繁体   English

将file1的每一行复制到file2的其他每行中(Python)

[英]Copy each line of file1 into every other line of file2 (Python)

Sorry for the ridiculous title; 对不起,标题太荒谬了; it's probably why I couldn't find an answer on Google. 这可能就是为什么我无法在Google上找到答案的原因。

I have 5 text files that I want to combine into 1. I'd like to have the format like this: 我有5个文本文件要合并为1。我想要这样的格式:

line1 of file1
line1 of file2
line1 of file3
line1 of file4
line1 of file5
line2 of file1
line2 of file2
line2 of file3
line2 of file4
line2 of file5

and so on. 等等。

I tried using the bash command below, but it seems like it's too much for sed or something: it just inserts the text into the first line, not the line of the variable I'm calling. 我尝试使用下面的bash命令,但对于sed或类似内容来说似乎太多了:它只是将文本插入到第一行,而不是我正在调用的变量的行。

for ((num=1; num<=66; num++)) ; do
    queryline=$(sed -n "${num}p" "file2.txt")
    sed -i "${num}i ${queryline}" "file1.txt"
done

(I tried this too) (我也尝试过)

for ((num=1; num<=66; num++)) ; do
    numa=$((num + 1))
    queryline=$(sed -n "${num}p" "file2.txt")
    sed -i "${numa}i ${queryline}" "file1.txt"
done

I'm thinking this might be easier with python (3.4), but I'm not sure how to do it. 我认为使用python(3.4)可能会更容易,但是我不确定如何做到这一点。 Tips please anyone? 提示请任何人?

Use contextlib.ExitStack() to handle the input files as a group and zip to read lines from all of the files: 使用contextlib.ExitStack()将输入文件作为一个组来处理,并使用zip来读取所有文件中的行:

import contextlib
import os

filenames = ['a','b','c','d','e']
output_file = 'fred'

# setup files for test
for filename in filenames:
    with open(filename, 'w') as fp:
        for i in range(10):
            fp.write('%s %d\n' % (filename, i))
if os.path.exists('fred'):
    os.remove('fred')

# open all the files and use zip to interleave the lines    
with open(output_file, 'w') as out_file, contextlib.ExitStack() as in_files:
    files = [in_files.enter_context(open(fname)) for fname in filenames]
    for lines in zip(*files):
        # if you're not sure last line has a \n
        for line in lines:
            out_file.write(line)
            if not line.endswith('\n'):
                out_file.write('\n')
        # if you are sure last line has a \n
        # out_file.write(''.join(lines))

print(open('fred').read())

If you are sure you have exactly 5 files, this will work. 如果确定只有5个文件,则可以使用。 If you need to make this work on a varying number of files, it gets a bit more complex. 如果您需要在不同数量的文件上执行此操作,它将变得更加复杂。

with open("file1.txt") as f:
    file1 = f.readlines()
with open("file2.txt") as f:
    file2 = f.readlines()
with open("file3.txt") as f:
    file3 = f.readlines()
with open("file4.txt") as f:
    file4 = f.readlines()
with open("file5.txt") as f:
    file5 = f.readlines()
outfile = open("outfile.txt", "w")
for aline in [line for foo in zip(file1, file2, file3, file4, file5) for line in foo]:
    outfile.write(aline)
outfile.close()

Your bash didn't work because you were trying to insert into a line which didn't exist before you insert. 您的bash无效,因为您尝试插入的行在插入之前不存在。

echo "\n" > file_to_insert.txt
for i in {1..5};do
  for((num=1;num<66;num++);do
    line_num=$((num*i)
    queryline=$(sed -n '${num}p' 'file${i}.txt'
    sed -i "${num}i '$queryline'" 'file_to_insert.txt'
done

Here is an gnu awk (gnu do to the ARGIND (file selector)) 这是一个gnu awk (对ARGIND (文件选择器)执行gnu do)

awk -v t=5 '{c=c<FNR?FNR:c; for (i=1;i<=t;i++) if (ARGIND==i) a[i FS FNR]=$0} END {for (i=1;i<=c;i++) for (j=1;j<=t;j++) print a[j FS i]}' file1 file2 file3 file4 file5

You set t to the number of files. 您将t设置为文件数。

Example: 例:

cat f1
file1 one
file1 two
file1 three
file1 four

cat f2
file2 one
file2 two
file2 three
file2 four

cat f3
file3 one
file3 two
file3 three
file3 four

awk -v t=3 '{c=c<FNR?FNR:c; for (i=1;i<=t;i++) if (ARGIND==i) a[i FS FNR]=$0} END {for (i=1;i<=c;i++) for (j=1;j<=t;j++) print a[j FS i]}' f1 f2 f3
file1 one
file2 one
file3 one
file1 two
file2 two
file3 two
file1 three
file2 three
file3 three
file1 four
file2 four
file3 four

How does it work? 它是如何工作的?

awk -v t=3 '                    # Set t to number of files
    {c=c<FNR?FNR:c              # Find the file with most records and store number in c
    for (i=1;i<=t;i++)      # Loop trough one and one file
        if (ARGIND==i)          # Test what file we are on
            a[i FS FNR]=$0}     # Stor data in array a
END {
    for (i=1;i<=c;i++)          # Loop trough line number
        for (j=1;j<=t;j++)      # Loop trough file number
            print a[j FS i]}    # Print data from array
' f1 f2 f3                      # Read the files

A good possibility to achieve what you want is to stick with standard utilities: here paste (specified by POSIX) is recommended: 要实现您想要的目标,一个很好的可能性就是坚持使用标准实用程序:在这里建议paste (由POSIX指定):

paste -d '\n' file1 file2 file3 file4 file5

or, if you like Bashisms: 或者,如果您喜欢Bashisms:

paste -d '\n' file{1..5}

This generalizes trivially to any number of files. 这可以简单地推广到任意数量的文件。

暂无
暂无

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

相关问题 使用 sed 或任何其他命令将部分行从 file1 复制到 file2 中的特定位置 - copy a part of a line from file1 to specific place in file2 using sed or any other command 如何删除文件1中出现在Python中的文件2中一次或多次的行? - How to delete a line in file1 that appeared once or multiple times in file2 in python? 使用 python 将多个列从 excel 表的 file1 复制到 file2 的其他列/行 其他 excel 表,使用 pandas 或其他任何东西 - copy multiple columns from file1 of an excel sheet to other column/row of file2 other excel sheet using python , use pandas or anything else 将N行从File1复制到File2,然后删除File1中的复制行 - Copy N lines from File1 to File2, then delete copied lines in File1 如果文件1中存在Python,如何删除文件2中的所有字符串? - How to delete all strings in file2 if exist in file1 with Python? 如何在python中显示file1到file2的变量内容 - how to display content of variables of file1 to file2 in python 获取文件1的相对路径(相对于文件2的路径,文件1在文件2的子文件夹中) - Get relative path of file1 (relative to path of file2, file1 is in subfolder of file2) 在python中逐行复制一个文件 - Copy a file line by line in python 在 file1 中查找单词并复制下一个单词并替换 file2 中的脚本 - Script to look for a word in file1 and copy the next word and replace that in file2 将一行的一部分与python中另一个文件中的每一行进行比较 - Comparing part of a line with every other line in another file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM