【问题标题】:Add quotes to all columns in csv files [closed]为 csv 文件中的所有列添加引号 [关闭]
【发布时间】:2019-03-02 21:22:54
【问题描述】:

我想要一个批处理文件来转换我的 csv 格式

value1,value2,value3

进入

"value1","value2","value3"

我尝试直接在 Excel 文件中执行相同操作(根据此答案 https://stackoverflow.com/a/38728364),但是当我保存文件时,我在记事本中看到它是

"""value1""","""value2""","""value3"""

只有使用 vba 脚本才有效,但使用起来不方便。 所以现在我正在寻找一些脚本,它可以比打开 excel、选择范围和上传 VBA 脚本来转换值更快地处理这个问题。我找到了 powershell 脚本,但在我的情况下它是无效的。我用谷歌搜索,只发现如何删除引号,而不是如何添加它们:D

您有任何其他想法如何在 Excel 中直接或在批处理文件中实现这一点?

【问题讨论】:

标签: excel csv batch-file


【解决方案1】:

您可以执行以下操作(请参阅说明性 rem 备注):

@echo off
rem // Read CSV file that is provided as command line argument:
for /F "usebackq delims=" %%L in ("%~1") do (
    rem // Store currently read line:
    set "LINE=%%L"
    rem /* Toggle delayed expansion to be able to write and read a variable in the same block of code
    rem    (without it reading a variable would return the value present before the whole block executes): */
    setlocal EnableDelayedExpansion
    rem /* Return the current line but enclose it within `""`
    rem    and replace every `,` by `","`;
    rem    this results in every comma -separated field to appear in between quotes: */
    echo("!LINE:,=","!"
    endlocal
)

这依赖于没有字段值本身包含逗号的假设。

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2020-02-07
    • 2020-11-05
    • 1970-01-01
    相关资源
    最近更新 更多