使用批处理文件从CSV文件中删除特殊字符

使用批处理文件从CSV文件中删除特殊字符

本文介绍了使用批处理文件从CSV文件中删除特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含18个字段的csv文件.我已经写了一个批处理文件来处理数据.除了从"DEVILS DUE/1FIRST COMICS,LLC"发布者中删除逗号之外,所有其他操作都有效.该字段无法正确解析.我曾尝试查看其他批处理文件作为示例,但我对snytax并不熟悉.

I have a csv file with 18 fields. I have written a batch file to manipulate the data. Everything works except removing the comma from the "DEVILS DUE /1FIRST COMICS, LLC" publisher. That field does not parse correctly. I have tried looking at other batch files for examples, but I am not familiar with the snytax.

@echo off & Setlocal EnableDelayedExpansion
( FOR /f "tokens=1-18 delims=," %%A in ('More +4 datatest.csv') do (
rem H is the department code
rem S is the sales tax code
rem Q is the publisher code
    Set "H=%%H"
    Set "S=T"
    Set "Q=%%Q"
    if "%%Q"=="BOOM! STUDIOS" Set "Q=BOOM STUDIOS"
    if "%%Q"=="DEVILS DUE /1FIRST COMICS, LLC" Set "Q=DEVILS DUE"
    if "%%H"=="1" Set "H=1005"
    if "%%H"=="1" Set "S=N"
    if "%%H"=="2" Set "H=1009"
    if "%%H"=="2" Set "S=N"
    if "%%H"=="3" Set "H=1008"
    if "%%H"=="4" Set "H=1002"
    if "%%H"=="5" Set "H=1006"
    if "%%H"=="6" Set "H=1003"
    if "%%H"=="7" Set "H=1011"
    if "%%H"=="8" Set "H=1011"
    if "%%H"=="9" Set "H=1004"
    if "%%H"=="10" Set "H=1016"
    if "%%H"=="11" Set "H=1015"
    if "%%H"=="12" Set "H=1015"
    if "%%H"=="13" Set "H=1011"
    if "%%H"=="14" Set "H=1009"
    if "%%H"=="14" Set "S=N"
    if "%%H"=="15" Set "H=1013"
    if "%%H"=="16" Set "H=1017"
    echo "",%%~M,%%~N,%%~L,"","","","","",!H!,"","",ITEM,"","",%%~D,%%Q,"","",%%E,"",%%E,"","","","","","","","","","","","","","",%%A,"","","","","",!S!,N,"","",DIAMOND,%%B,"",""
  )
)>paygoinvoice.csv
@echo on

推荐答案

您遇到的问题是,您在 FOR 循环中使用的 delims =,导致将代码中的 DEVILS DUE/1FIRST COMICS,LLC ANY 逗号更改为空格.

The issue you're having is that the delims=, you have in your FOR loop is causing to change DEVILS DUE /1FIRST COMICS, LLC or ANY comma's in your code as a space.

使用 Tokens = 然后 %% H = DEVILS DUE/1FIRST COMICS 组合------ %% I = LLC .

Combine this with Tokens= then %%H = DEVILS DUE /1FIRST COMICS ---And--- %%I = LLC.

一个快速而又肮脏的修复程序(emem)(我知道)是将所有," 更改为不同的东西,然后再运行到主函数中.在我的示例中,我使用了 1Comma1 .这会将您的 IF 搜索更改为 DEVILS DUE/1FIRST COMICS1Comma1 LLC .

A quick and dirty fix (That I know of) is to simply change all ", " to something different before running it into the main function. For my example I used 1Comma1. This will change your IF search to DEVILS DUE /1FIRST COMICS1Comma1 LLC.

已修复.蝙蝠:

@echo off & Setlocal EnableDelayedExpansion

Rem | Replace all ", " with "1Comma1"
for /f "tokens=1,* delims=¶" %%A in ('"type datatest.csv"') do (
    SET string=%%A
    setlocal EnableDelayedExpansion
    SET modified=!string:, =1Comma1 !

    >> datatest.csv.TEMP echo(!modified!
    endlocal
)

Rem | Main .CSV Edit Function
( FOR /f "tokens=1-8* delims=," %%A in ('More +4 datatest.csv.TEMP') do (
    Set "ItemData=%%H"
    if "%%H"=="1" Set "ItemData=1005"
    if "%%H"=="3" Set "ItemData=1008"
    if "%%H"=="BOOM STUDIOS" Set "ItemData=NEW STUDIOS"
    if "%%H"=="DEVILS DUE /1FIRST COMICS1Comma1 LLC" Set "ItemData=DEVILS DUE"

    echo %%A,%%B,%%C,%%D,%%E,%%F,%%G,!ItemData!,%%I
  )
)>paygoinvoice.txt
del datatest.csv.TEMP

@echo on

PS:我在上面的示例中使用的代码摘自您最近关于该主题的帖子.只需将新代码添加到其所属位置即可.

PS: The code I used for your example above was taken from your last post about the subject. Simply add your new code where it belongs.

还请记住, EnableDelayedExpansion 会自动从 FOR 循环或 IF 语句的输出中删除

Also keep in mind that EnableDelayedExpansion auto removes ! from the output of an FOR loop or IF statement.

这篇关于使用批处理文件从CSV文件中删除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:40