批处理脚本:更快的ECHO输出到FOR循环中的csv文件? [英] Batch Script: Quicker ECHO output to a csv file in a FOR loop?

查看:726
本文介绍了批处理脚本:更快的ECHO输出到FOR循环中的csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个脚本,将文本文件中的分号分隔符替换为逗号,但是它也在第9列的文本中引用了引号,因为第9列包含逗号。当我输出到新的文本文件,它的过程非常缓慢,可能需要4-5分钟,它正在读取的文本文件是50MB。有更快的方式或更有效的方式做到这一点吗?这是我的FOR循环:

I wrote a script that replaces the semicolon delimiter in a text file to commas, but it also puts quotes around the text of the 9th column since the 9th column contains commas. When I output to the new text file it process very slow, takes maybe 4-5 minutes, the text file that it is reading from is 50MB. Is there faster way or more efficient way to do this? Here is my FOR loop:

FOR /f "usebackq tokens=1-9* delims=;" %%a IN ("%FILENAME%") DO (
SET C10=%%j
ECHO(%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,"%%i",!C10:;=,! >> "%MYPATH%\Filename %MMDDYYYY%.csv")

或者我应该只是学习python .....

or should I just learn python.....

谢谢。

推荐答案

让脚本运行速度更快的一件事是避免打开和关闭每个写入操作的输出文件

One thing that will make the script run "faster" is to avoid opening and closing the output file for each write operation

>> "%MYPATH%\Filename %MMDDYYYY%.csv" (
    FOR /f "usebackq tokens=1-9* delims=;" %%a IN ("%FILENAME%") DO (
        SET C10=%%j
        ECHO(%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,"%%i",!C10:;=,!
    )
)

不重定向每行, c> 命令。

Instead of redirecting each line, redirect the full for command.

这篇关于批处理脚本:更快的ECHO输出到FOR循环中的csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆