首页 文章

批处理脚本 - Txt到CSV,如何将文本限定符添加到列?

提问于
浏览
0

我试图让这个脚本自动将文本文件转换为CSV,但它由管道符“|”分隔 . 我下面的内容是在我的批处理文件中,它用逗号替换管道,我唯一的问题是,在第7列中,字符串包含逗号,所以当我在Excel中打开CSV文件时,它将这些逗号视为列并混淆列的格式 . 有没有办法只将文本限定符添加到第7列?文本限定符是字符串周围的引号 .

值1 |值2,AndSome |值3

有脚本将文本文件转换为:

值1, “值2,AndSome”,值3

@echo off
setLocal enableDELAYedexpansion
for /f "tokens=* delims=^|" %%a in (myFile.txt) do (
set str=%%a
echo !str:^|=,! >> myFile.csv
)

2 回答

  • 2
    @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET "sourcedir=U:\sourcedir"
    SET "filename1=%sourcedir%\q35002863.txt"
    FOR /f "usebackqtokens=1-7*delims=^|" %%a IN ("%filename1%") DO (
     SET "C8=%%h"
     ECHO(%%a,%%b,%%c,%%d,%%e,%%f,"%%g",!C8:^|=,!
    )
    
    GOTO :EOF
    

    这应该可以解决您的问题 .

  • 2

    JREPL.BAT是一个功能强大的正则表达式查找/替换实用程序,可以轻松有效地解决此问题 . 它是纯脚本(混合JScript /批处理),可以在XP之后的任何Windows机器上本机运行 .

    我可以写一个总是引用第7列的解决方案,但这个用途有限 . 更强大的解决方案是选择性地引用包含逗号的任何列,而不管位置如何 . 任何没有逗号的列都将保持不加引号 .

    jrepl "\| [^|,]*,[^|]*" ", \q$&\q" /t " " /x /f myFile.txt /o myFile.csv
    

    唯一可能让你失望的事情是,如果任何列已经包含引号 . CSV "standard"要求将任何引用文字转义为 "" ,并且该列也用引号括起来 . 以下内容将正确地转义引号文字,并且还包含引号内包含逗号或引号的任何列 .

    jrepl "\| [^|,]*[,\x22][^|]*" "',' '\x22'+$0.replace(/\x22/g,'\x22\x22')+'\x22'" /t " " /j /f myFile.txt /o myFile.out
    

    可以添加的最后一件事是将命令放在批处理脚本中,并参数化分隔符,源文件和目标文件 . 我还在脚本中添加了一个帮助工具 .

    delim2csv.bat

    ::
    ::delim2csv  Delimiter  InFile  [OutFile]
    ::delim2csv  /?
    ::
    ::  Convert a delimited text file into a CSV file, where
    ::    - columns containing comma or quote are quoted
    ::    - quote literals are doubled
    ::    - Delimiter characters are converted to commas
    ::
    ::  The OutFile is optional. The result will be written to stdout
    ::  if the OutFile is not specified. Use - for the OutFile to
    ::  overwrite the InFile with the result.
    ::
    ::  Remember that the delimiter is used in a regular expression,
    ::  so the character must be escaped if it is a regex meta character,
    ::  or encoded if it is difficult to represent on the command line.
    ::  Any extended ASCII character may be specified by using \xNN,
    ::  where NN is the hexidecimal representation of the character code.
    ::  Enclosing argument quotes will be removed before use in the regex.
    ::
    ::  Example Delimiters: pipe =  "\|"  or  \x7C
    ::                      tab  =   \t   or  \x09
    ::
    ::  If the first argument is /?, then this help documentation will
    ::  be written to stdout.
    ::
    ::  This script requires JREPL.BAT to function, available at:
    ::  http://www.dostips.com/forum/viewtopic.php?t=6044
    ::
    @echo off
    if "%~1" equ "/?" (
      for /f "delims=: tokens=1*" %%A in ('findstr /n "^::" "%~f0"') do echo(%%B
      exit /b
    )
    @call jrepl "%~1 [^%~1,]*[,\x22][^%~1]*"^
                "',' '\x22'+$0.replace(/\x22/g,'\x22\x22')+'\x22'"^
                /t " " /j /f %2 /o %3
    

    因此,使用上面的脚本,解决方案将变为:

    delim2csv "\|" MyFile.txt MyFile.csv
    

    EDIT 2017-02-19

    https://stackoverflow.com/a/42324094/1012053我开发了一个名为parseCSV.bat的小型混合脚本,专门用于转换CSV数据,并且不使用正则表达式 . 它比依赖JREPL.BAT的上述解决方案快11倍 . 正则表达式功能强大,方便且简洁,但手工构造的代码通常更快 .

    使用parseCSV.bat,解决方案就变成了

    parseCSV "/I:|" /L /Q:E <MyFile.txt >MyFile.csv
    

    输出中唯一的区别是parseCSV引用每个列值,但delim2csv仅引用包含逗号或引号的列值 .

相关问题