2016-01-25 53 views
0

我試圖讓這個腳本自動將文本文件轉換爲CSV文件,但是它由管道字符「|」分隔。我的下面是我的批處理文件,它用逗號取代管道,我唯一的問題是,在第7列中,字符串包含逗號,所以當我在Excel中打開CSV文件時,它將這些逗號視爲列並混淆列的格式。有沒有辦法只添加文本限定符到第7列?文本限定符在字符串周圍引號。批處理腳本--Txt到CSV,如何將文本限定符添加到列?

值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

如果您唯一的問題是在Excel中正確打開文件,您可以在第一行添加'sep = |',告訴Excel哪個字符用作字段分隔符(該行不會是Excel工作表) – Stephan

回答

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 

這應該可以解決你的問題。

+0

工程就像一個魅力。謝謝! – MPineda

2

JREPL.BAT是一個功能強大的正則表達式查找/替換工具,可以輕鬆高效地解決此問題。它是純粹的腳本(混合JScript /批處理),可以從XP以後的任何Windows機器上本機運行。

我可以編寫一個總是引用第7列的解決方案,但這種解決方案的用途有限。更強大的解決方案將有選擇地引用任何包含逗號的列,而不管位置如何。任何沒有逗號的列將保持不加引號。

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

唯一可能會讓你失望的其他事情是如果任何一列已經包含報價。 CSV「標準」要求任何引號字面值被轉義爲"",並且該列也用引號引起來。以下內容將正確地轉義引用文字,並附上任何包含逗號或引號的列。

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 

編輯2017年2月19日

過在https://stackoverflow.com/a/42324094/1012053我開發了一個小型混合動力車腳本名爲parseCSV.bat,專門用於轉換CSV數據,並且不使用正則表達式。它比依賴於JREPL.BAT的上述解決方案快11倍以上。正則表達式功能強大,方便,簡潔,但手工構建的代碼通常更快。

用parseCSV。蝙蝠,溶液變成

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

在輸出的唯一區別是parseCSV引用每一列的值,但是隻有delim2csv引用包含逗號或報價列值。

相關問題