让我描述一下我的问题。我有一个从 excel 导出的包含大量数据的 csv 文件。该文件在第一行有标题,在第二行有列标题。我只需要从该文件中提取两列(第 2 列和第 3 列),将它们放入 1 列并将输出发送到另一个文件。
例子:
Title
colA , colB , colC , colD ,...
abc , def , ghi , jkl ,...
abc , def , ghi , jkl ,...
abc , def , ghi , jkl ,...
abc , def , ghi , jkl ,...
问题是,当 csv 解析器遇到包含带有 - ( ) @ 字符的字符串的行时,它会失败。(循环将它们视为我认为的分隔符,因此每次都会给我一个超出范围的错误)。
这是我已经拥有的。
@Echo off & setlocal EnableExtensions
setLocal EnableDelayedExpansion
REM creating and clearing files
copy /y NUL C:\list1.csv >NUL
copy /y NUL C:\list1_tmp.csv >NUL
copy /y NUL C:\exportedColumns.csv >NUL
copy /y NUL C:\Result.txt >NUL
set Result=C:\Result.txt
set Source=C:\sourcelist.csv
set list1=C:\list1.csv
set list1_tmp=C:\list1_tmp.csv
set expCol=C:\exportedColumns.csv
REM skip 1st two lines from source file and put to output file list1
for /f "skip=2 delims=*" %%a in (%Source%) do (echo %%a >>%list1%)
REM shorten each line to 500 chars and put it to new file
for /f "tokens=* delims=" %%a in ("%list1%") do (
set s=%%a
set s=%s:~0,500%
echo.%s% >> "%list1_tmp%"
)
REM ^^^^^^^^^^^ this is not working. It puts only 1 space to the output file
rem Parsing the csv file
rem Process the file:
call :ProcessFile < %list1_tmp%
exit /B
:ProcessFile
set /P line=
:nextLine
set line=:EOF
set /P line=
if "!line!" == ":EOF" goto :EOF
set i=0
for %%e in (%line%) do (
set /A i+=1
for %%i in (!i!) do (
if %%i==1 echo %%~e >> %expCol%
if %%i==2 echo %%~e >> %expCol%
)
if %%i==3 goto nextLine
REM I don't want it to process all the columns
)
goto nextLine
我想请您看看这个并帮助我将 2 列合二为一并将输出放入 1 个文件。
我将不胜感激。