合并 3 个自定义格式的 txt 文件

合并 3 个自定义格式的 txt 文件

我有 3 个文件

1-包含 600 行“案例 1:”、“案例 2:”、“案例 3:”

2-包含600行“pictureBox1.Image = Properties.Resources._1;”

3- 包含600行“Break”;

我想将它们全部合并到这个格式中

                case 1:
                    pictureBox1.Image = Properties.Resources._1;
                    break;
                case 2:
                    pictureBox1.Image = Properties.Resources._2;
                    break;
                case 3:
                    pictureBox1.Image = Properties.Resources._3;
                    break;
                case 4:
                    pictureBox1.Image = Properties.Resources._4;
                    break;
                case 5:
                    pictureBox1.Image = Properties.Resources._5;
                    break;

这甚至可以与自定义格式合并吗?

答案1

是的,可以将这三个文件合并为您描述的格式。您可以使用一个简单的脚本来执行此操作。以下是使用 Python 执行此操作的示例:

# Open the first file in read mode
with open('file1.txt', 'r') as file1:
  # Open the second file in read mode
  with open('file2.txt', 'r') as file2:
    # Open the third file in read mode
    with open('file3.txt', 'r') as file3:
      # Open a new file in write mode
      with open('output.txt', 'w') as output:
        # Iterate over the lines in the first file
        for line1 in file1:
          # Read the corresponding lines from the second and third files
          line2 = file2.readline()
          line3 = file3.readline()
          
          # Format the lines as described in the question
          output_line = '                ' + line1.strip() + ':\n'
          output_line += '                    ' + line2.strip() + ';\n'
          output_line += '                    ' + line3.strip() + ';\n'
          
          # Write the formatted line to the output file
          output.write(output_line)

该脚本从第一个文件中读取每一行,然后从第二个和第三个文件中读取相应的行。然后按照问题中的描述格式化这些行,并将结果写入名为 output.txt 的新文件。

您可以调整代码以使用不同的文件名并添加所需的任何其他格式。

相关内容