Board logo

标题: [文本处理] [分享]批处理把键值格式转换成表格结构另存为csv文件 [打印本页]

作者: Batcher    时间: 2023-7-4 22:25     标题: [分享]批处理把键值格式转换成表格结构另存为csv文件

【问题描述】

原文件数据结构是key:value格式,我想转换成表格结构,文件另存为csv格式,写不下去了。
有12个这样txt文件,我想把每个文件都转换了,另存为csv格式。



国债204001-5245.txt
  1. Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
  2. Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
  3. Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
  4. Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
  5. Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
  6. Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
  7. Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
  8. Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
  9. Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
复制代码

作者: Batcher    时间: 2023-7-4 22:25

【解决方案】

test-1.bat
  1. @echo off
  2. for %%U in ("*5245.txt") do (
  3.     >"%%~nU.csv" echo,Date,SmallNetTur,MidNetTur,BigNetTur,SuperNetTur
  4.     (for /f "tokens=1-10 delims=:," %%a in ('type %%U') do (
  5.         echo,%%b,%%d,%%f,%%h,%%j
  6.     ))>>"%%~nU.csv"
  7. )
复制代码

作者: Batcher    时间: 2023-7-4 22:26

test-2.bat
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for %%U in ("*5245.txt") do (
  4.     set /p FirstRow=<"%%U"
  5.     (for /f "tokens=1-10 delims=:," %%a in ("!FirstRow!") do (
  6.         echo,%%a,%%c,%%e,%%g,%%i
  7.     ))>"%%~nU.csv"
  8.     (for /f "tokens=1-10 delims=:," %%a in ('type "%%U"') do (
  9.         echo,%%b,%%d,%%f,%%h,%%j
  10.     ))>>"%%~nU.csv"
  11. )
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2