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

查看:28
本文介绍了批处理脚本:在 FOR 循环中更快地 ECHO 输出到 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:;=,!
    )
)

不是重定向每一行,而是重定向完整的 for 命令.

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

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

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