【问题标题】:How to merge 3 text files in one (Line1 of Text1+Line1 of Text2+Line1 of Text3) [duplicate]如何将3个文本文件合并为一个(文本1的第1行+文本2的第1行+文本3的第1行)[重复]
【发布时间】:2020-02-08 10:33:31
【问题描述】:

我有 3 个文本文件,每个文件包含 10 行:

文本1:

01-TextA
02-TextA
03-TextA
04-TextA
05-TextA
06-TextA
07-TextA
08-TextA
09-TextA
10-TextA

文本2:

01-TextB
02-TextB
03-TextB
04-TextB
05-TextB
06-TextB
07-TextB
08-TextB
09-TextB
10-TextB

文本3:

01-TextC
02-TextC
03-TextC
04-TextC
05-TextC
06-TextC
07-TextC
08-TextC
09-TextC
10-TextC

如何在一个新的文本文件中组合 text1 的第 1 行 + text2 的第 1 行 + text3 的第 1 行等。 它应该是这样的:

01-TextA
01-TextB
01-TextC
02-TextA
02-TextB
02-TextC
03-TextA
03-TextB
03-TextC
04-TextA
04-TextB
04-TextC
05-TextA
05-TextB
05-TextC

【问题讨论】:

  • 你想用什么编程语言?
  • 你试过什么?基本上你需要同时遍历 3 个文件中的行
  • NVM,我看到了,批处理文件。
  • 是否可以使用 Windows 批处理文件来实现这一点?还是用excel?
  • 所以,我猜在输出文件中你会得到 30 行而不是 15 行??

标签: file batch-file join text merge


【解决方案1】:

我个人会使用 python 来读取 json 文件并从创建 3 个列表开始,每个列表都是原始 json 文件之一。

json1_file = open('json1')
json1_str = json1_file.read()
... Same for json2
... Same for json3

然后创建您的最终列表并像下面这样一次迭代每个列表的元素 1 级。

final_list = [] 

for i in range(0,len(int(json1_str))):
    final_list[].append = json1_str[i]

或者类似的东西。抱歉,如果格式不正确或当前代码中存在任何错误,但我在手机上。如果这不起作用,我将使用正确的代码进行更新,但是我认为这至少可以让你一开始就走上正轨。

【讨论】:

  • 谢谢你的回答,但是我不懂Python语言。你认为会有更简单的方法吗?像 excel 脚本,还是 Windows 批处理文件?
  • 我会说python将是最简单的方法...如果您参与学习python的基础知识会非常方便。网上有很多信息可以浏览所有内容。我以前使用过 VBA 和 Python,它们都非常适合此类任务,但根据经验,python 更容易学习、使用并且更快地获得预期的结果。
  • 除了学习python之外,你可以在线将json文件转换成csv然后使用excel吗?
【解决方案2】:

希望你想用这段代码得到什么:

@echo off
set "File1=Text1.txt"
set "File2=Text2.txt"
set "File3=Text3.txt"
Set "OutPutFile=%~dp0OutPut.txt"
If Exist "%OutPutFile%" Del "%OutPutFile%"
set /a count1=0
set /a count2=0
set /a count3=0
SETLOCAL enabledelayedexpansion
for /F "tokens=* delims=" %%a in ('Type "%File1%"') do (
         Set /a count1+=1
         Set "LineTxt1[!count1!]=%%a"     
)

for /F "tokens=* delims=" %%b in ('Type "%File2%"') do (
         Set /a count2+=1
         Set "LineTxt2[!count2!]=%%b"     
)

for /F "tokens=* delims=" %%c in ('Type "%File3%"') do (
         Set /a count3+=1
         Set "LineTxt3[!count3!]=%%c"     
)

For /L %%i in (1,1,%Count1%) Do (
    Call :Write !LineTxt1[%%i]! !LineTxt2[%%i]! !LineTxt3[%%i]!
)
Start "" "%OutPutFile%"
Exit
::*******************************************************
:Write
(
    echo %1 
    echo %2
    echo %3
)>>"%OutPutFile%"
exit /b
::*******************************************************

输出是这样的:

01-TextA 
01-TextB
01-TextC
02-TextA 
02-TextB
02-TextC
03-TextA 
03-TextB
03-TextC
04-TextA 
04-TextB
04-TextC
05-TextA 
05-TextB
05-TextC
06-TextA 
06-TextB
06-TextC
07-TextA 
07-TextB
07-TextC
08-TextA 
08-TextB
08-TextC
09-TextA 
09-TextB
09-TextC
10-TextA 
10-TextB
10-TextC

【讨论】:

  • 太棒了。非常感谢!
【解决方案3】:

在cmd窗口(或bat文件)输入

复制文本1 + 文本2 + 文本3 文本4
输入文本4 |排序 > text5

您将收到已排序的文件文本5

【讨论】:

  • 3 个文本文件的内容不固定,可能会有所不同。它将取决于 text1.txt + text2.txt + text3.txt
  • 在任何情况下,您都会收到文件 5,其中您将按排序顺序从文件 1、2、3 中获取所有行
  • 我不明白你解释的怎么做。我有 3 个文件:text1.txt、text2.txt 和 text3.txt
  • 打开 cmd 窗口。在键盘上输入:copy text1 + text2 + text3 text4 按 Enter 在键盘上输入:type text4 | sort > text5 按 Enter 就是这样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多