批处理 - 似乎无法读取CSV文件

时间:2016-12-21 20:38:36

标签: csv parsing batch-file

我对文本文件没有任何问题,但是当我尝试使用csv做一些简单的事情时,它不起作用!

可以在此处找到csv:https://1drv.ms/u/s!At4HWeqiFYNvhuo4ibcifbvPwO_4wA

Name,Type,Value,Designate,Access,Source,Description,Restricted,Unit Quantity,Unit
TITLE_1,String,PCB ASSY,Yes,Full,User-Defined,,,,

代码:

for /f "usebackq delims=" %%a in (%FILENAME%) DO (
    Set FileLine=%%a
    echo !FileLine!
)

有人可以帮忙!谢谢!

4 个答案:

答案 0 :(得分:1)

Notepad ++表示您的文件是使用BOM在UCS2 LE中编码的。我将编码更改为ANSI并且您的代码工作正常。对于它的价值,type命令似乎更能容忍该编码和字节顺序标记。

@echo off & setlocal

set "file=param_table.csv"

for /f "delims=" %%I in ('type "%file%"') do (
    set "line=%%I"
    setlocal enabledelayedexpansion
    echo(!line!
    endlocal
)

答案 1 :(得分:0)

与rojo有同样的问题,只是为了显示delims=,解析我的批次

@Echo off
Set FileNAme=".\param_table.csv"
Echo whole lines
for /f "delims=" %%a in ('type %FILENAME%') DO (
    Set FileLine=%%a
    echo !FileLine!
)
Echo:
Echo split into fields/columns reversed order
for /f "tokens=1-10 delims=," %%a in ('type %FILENAME%') DO (
    echo %%j,%%i,%%h,%%g,%%f,%%e,%%d,%%c,%%b,%%a
)

输出

whole lines
Name,Type,Value,Designate,Access,Source,Description,Restricted,Unit Quantity,Unit
TITLE_1,String,PCB ASSY,Yes,Full,User-Defined,,,,

split into fields/columns reversed order
Unit,Unit Quantity,Restricted,Description,Source,Access,Designate,Value,Type,Name
,,,,User-Defined,Full,Yes,PCB ASSY,String,TITLE_1

我的hex vbscript显示了这个:

Detected Byte Order Mark (BOM) : UTF16LE

Blk00000: File : param_table.csv
HexOffs.:_+0_+1_+2_+3__+4_+5_+6_+7__+8_+9_+A_+B__+C_+D_+E_+F__----____----____
00000000: FF FE 4E 00  61 00 6D 00  65 00 2C 00  54 00 79 00  ..N.a.m.e.,.T.y.
00000010: 70 00 65 00  2C 00 56 00  61 00 6C 00  75 00 65 00  p.e.,.V.a.l.u.e.
00000020: 2C 00 44 00  65 00 73 00  69 00 67 00  6E 00 61 00  ,.D.e.s.i.g.n.a.
00000030: 74 00 65 00  2C 00 41 00  63 00 63 00  65 00 73 00  t.e.,.A.c.c.e.s.
00000040: 73 00 2C 00  53 00 6F 00  75 00 72 00  63 00 65 00  s.,.S.o.u.r.c.e.

答案 2 :(得分:0)

我也用过

powershell "get-content !CSVFileName!" >!TXTFileName!

只需转换确切的文件而无需重新格式化。因此,当值本身中有逗号,冒号,引号时,我能够解释任何解析问题;在我自己的代码中。

答案 3 :(得分:-1)

感谢Rojo,我正在使用

powershell "ipcsv !CSVFileName!" >!TXTFileName!