[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 【已解决】求助从CSV中读取内容后重新整理成一行行的

本帖最后由 zhengwei007 于 2024-3-15 09:42 编辑

我有一个rec.csv文件,里面内容如下:
  1. 3 corpse_make_list={{[charcoal];1;1;5.6617};{[magic_ring];1;1;34.3131};{[rp_broad_sword];1;1;4.5293}} additional_make_multi_list={{{{[adena];30;42;100}};70};{{{[apprentice's_earing];1;1;30.9859};{[necklace_of_magic];1;1;23.0047};{[magic_ring];1;1;46.0094}};25.7009};{{{[stem];1;1;29.1262};{[varnish];1;1;14.5631};{[suede];1;1;9.7087};{[silver_nugget];1;1;5.8252};{[thread];1;1;29.1262};{[rp_bow];1;1;11.6506}};6.6988}}
  2. 4 corpse_make_list={{[suede];1;1;5.1827};{[willow_staff_head];1;1;0.5553};{[broad_sword_blade];1;1;0.5553}} additional_make_multi_list={{{{[adena];41;58;100}};70};{{{[cotton_shoes];1;1;33.3333};{[crude_leather_shoes];1;1;33.3333};{[short_leather_gloves];1;1;33.3334}};1.5132};{{{[charcoal];1;1;50};{[suede];1;1;33.3333};{[rp_wooden_arrow];1;1;16.6667}};3.0618}}
  3. 5 corpse_make_list={{[lesser_healing_potion];1;1;21.6259};{[suede];1;1;6.4878};{[rp_wooden_arrow];1;1;3.2439}} additional_make_multi_list={{{{[adena];51;72;100}};70};{{{[small_shield];1;1;58.8121};{[leather_cap];1;1;41.1879}};1.5322};{{{[coal];1;1;13.9182};{[varnish];1;1;13.9182};{[rp_cedar_staff];1;1;2.5727};{[wooden_arrow];10;30;69.5909}};10.3267}}
复制代码
上面代码是以制表符分隔的一共3行3列。
如果英文名称后面是4组数字,则将数字直接写在后面,然后另起一行。
如果英文名称后面是3组数字,则将将后面出现4组数字的最后一组,写到前面的最后一组给补齐即可。
通过整理希望达到如下效果:
  1. 3 corpse_make_list charcoal 1 1 5.6617
  2. 3 corpse_make_list magic_ring 1 1 34.3131
  3. 3 corpse_make_list rp_broad_sword 1 1 4.5293
  4. 3 additional_make_multi_list adena 30 42 100 70
  5. 3 additional_make_multi_list apprentice's_earing 1 1 30.9859 25.7009
  6. 3 additional_make_multi_list necklace_of_magic 1 1 23.0047 25.7009
  7. 3 additional_make_multi_list magic_ring 1 1 46.0094 25.7009
  8. 3 additional_make_multi_list stem 1 1 29.1262 6.6988
  9. 3 additional_make_multi_list varnish 1 1 14.5631 6.6988
  10. 3 additional_make_multi_list suede 1 1 9.7087 6.6988
  11. 3 additional_make_multi_list silver_nugget 1 1 5.8252 6.6988
  12. 3 additional_make_multi_list thread 1 1 29.1262 6.6988
  13. 3 additional_make_multi_list rp_bow 1 1 11.6506 6.6988
  14. 4 corpse_make_list suede 1 1 5.1827
  15. 4 corpse_make_list willow_staff_head 1 1 0.5553
  16. 4 corpse_make_list broad_sword_blade 1 1 0.5553
  17. 4 additional_make_multi_list adena 41 58 100 70
  18. 4 additional_make_multi_list cotton_shoes 1 1 33.3333 1.5132
  19. 4 additional_make_multi_list crude_leather_shoes 1 1 33.3333 1.5132
  20. 4 additional_make_multi_list short_leather_gloves 1 1 33.3334 1.5132
  21. 4 additional_make_multi_list charcoal 1 1 50 3.0618
  22. 4 additional_make_multi_list suede 1 1 33.3333 3.0618
  23. 4 additional_make_multi_list rp_wooden_arrow 1 1 16.6667 3.0618
  24. 5 corpse_make_list lesser_healing_potion 1 1 21.6259
  25. 5 corpse_make_list suede 1 1 6.4878
  26. 5 corpse_make_list rp_wooden_arrow 1 1 3.2439
  27. 5 additional_make_multi_list adena 51 72 100 70
  28. 5 additional_make_multi_list small_shield 1 1 58.8121 1.5322
  29. 5 additional_make_multi_list leather_cap 1 1 41.1879 1.5322
  30. 5 additional_make_multi_list coal 1 1 13.9182 10.3267
  31. 5 additional_make_multi_list varnish 1 1 13.9182 10.3267
  32. 5 additional_make_multi_list rp_cedar_staff 1 1 2.5727 10.3267
  33. 5 additional_make_multi_list wooden_arrow 10 30 69.5909 10.3267
复制代码
我希望通过批处理,执行后自动l输出到sour.csv中即可。

为什么前三行是三组数字?

QQ 20147578

TOP

本帖最后由 zhengwei007 于 2024-3-14 16:30 编辑
为什么前三行是三组数字?
czjt1234 发表于 2024-3-14 06:57


自身的格式就是这样,所以根据前面的英文,corpse_make_list这个类型就是3组数字,整理出来后,这个一行是5个字段,而additional这个开头的,是6个字段。

TOP

回复 1# zhengwei007

再用纯P版来一轮“庖丁解牛”...
用一楼示例测试通过
  1. @echo off &setlocal enabledelayedexpansion
  2. (for /f "tokens=1-5 delims== " %%1 in (rec.csv) do (
  3. set "vc=%%3"&set "vc=!vc:};{=,!"&set "vc=!vc:;=/!"
  4. for %%c in ( {,[,],} ) do (set "vc=!vc:%%c=!")
  5. for %%e in (!vc!) do (set "_vc=%%e"&echo,%%1 %%2 !_vc:/= !)
  6. set "va=%%5"&set "va=!va:~5!"&set "va=!va:{{[=,!"&set "va=!va:;=/!"
  7. for %%a in (!va!) do (
  8. set "av=%%a"&set "vn=!av:*}}/=!"&for %%c in ( {,/,} ) do (set "vn=!vn:%%c=!")
  9. for %%q in (!vn!) do (set "_av=!av:/%%q=!")
  10. set "_av=!_av:}/{=,!"
  11. for %%c in ( {,[,],} ) do (set "_av=!_av:%%c=!")
  12. for %%e in (!_av!) do (set "v=%%e"&echo,%%1 %%4 !v:/= ! !vn!)
  13. )
  14. ))>"sour.csv"
  15. endlocal&exit/b
复制代码

TOP

一楼测试通过。
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. (for /f "delims=" %%a in (res.csv) do (call :str1 "%%a"))>a.log
  4. (for /f "delims=" %%a in (a.log) do (call :str2 "%%a"))>b.log
  5. (for /f "tokens=1* delims==" %%i in (b.log) do (if "%%j" neq "" (echo %%i& echo %%j) else echo %%i))>a.log
  6. for /f "tokens=1-7* delims=[]{}; " %%1 in (a.log) do (
  7. ::echo %%1 %%2 %%3 %%4 %%5
  8. if "%%2" == "" (echo %%1|findstr /v "[^0-9]" >nul&&set v1=%%1||set v2=%%1
  9. ) else (
  10. if "!v2!" == "corpse_make_list" (echo !v1!, !v2!, %%1, %%2, %%3, %%4
  11. ) else (
  12. if "%%5" == "" (
  13. echo  m!n!=!v1!, !v2!, %%1, %%2, %%3, %%4 >>$
  14. set /a n+=1
  15. ) else (
  16. for /f "tokens=2 delims==" %%a in ('type "$"') do echo %%a, %%5
  17. echo !v1!, !v2!, %%1, %%2, %%3, %%4, %%5
  18. del /q $ 2>nul
  19. )
  20. )
  21. )
  22. )>>sour.csv
  23. del /q a.log b.log
  24. exit
  25. :str1
  26. set "str=%~1"
  27. echo;%str: =&echo %
  28. goto:eof
  29. :str2
  30. set "str=%~1"
  31. echo;%str:};{=&echo %
  32. goto:eof
复制代码

TOP

本帖最后由 WHY 于 2024-3-15 17:06 编辑
  1. @if(0)==(0) echo off
  2. type 1.txt | cscript -nologo -e:jscript "%~f0" > 2.txt
  3. pause & exit
  4. @end
  5. var reg = /\[[^[\]]+\](?:;\d+(?:\.\d+)?){3}\}*(;\d+(?:\.\d+)?)?/g;
  6. while (!WSH.StdIn.AtEndOfStream) {
  7.     var arr = WSH.StdIn.ReadLine().split('\t');
  8.     var s0 = arr[0];                                //第1列
  9.     for (var i=1; i<arr.length; i++) {
  10.         var s1 = arr[i].split('=')[0];              //第2列
  11.         var ret = [];
  12.         while (m = reg.exec(arr[i])) {
  13.             var s2 = m[0].replace(/[[\]}]/g, '').replace(/;/g, '\t'); //第3-6列
  14.             s2 = s0 + '\t' + s1 + '\t' + s2;
  15.             ret.push(s2);
  16.             if (m[1] != '') {
  17.                 var s4 = m[1].replace(';', '\t');   //第4个数字列
  18.                 for (var j=0; j<ret.length-1; j++) ret[j] += s4;
  19.                 WSH.Echo(ret.join('\r\n'));
  20.                 ret = [];
  21.             }
  22.         }
  23.         if (ret.length) WSH.Echo(ret.join('\r\n'));
  24.     }
  25. }
复制代码

TOP

谢谢楼上几位,搞定啦

TOP

回复 1# zhengwei007
将以下文本以ANSI编码格式保存为modi_rec_csv.awk,下载gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe )后执行 gawk -fmodi_rec_csv.awk rec.csv>sour.csv即可:
  1. BEGIN {
  2. OFS = "\t"
  3. }
  4. {
  5. A = $1
  6. B = gensub(/^(.+)=.+$/, "\\1", "1", $2)
  7. C = gensub(/^[^]\[\]]+(({\[[^]\[\]]+\];[0-9]+;[0-9]+;[0-9]+(\.[0-9]+)?};?)+)[^]\[\]]+$/, "\\1", "1", $2)
  8. n = patsplit(C, c, /[^\[\];\{\}]+/)
  9. for (i = 1; i <= n; i += 4) {
  10. print A, B, c[i], c[i + 1], c[i + 2], c[i + 3] OFS
  11. }
  12. B = gensub(/^(.+)=.+$/, "\\1", "1", $3)
  13. n = patsplit($3, c, /{(({\[[^]\[\]]+\];[0-9]+;[0-9]+;[0-9]+(\.[0-9]+)?};?)+)};[0-9]+(\.[0-9]+)?/)
  14. for (i = 1; i <= n; i++) {
  15. m = patsplit(c[i], d, /[^\[\];\{\}]+/)
  16. for (j = 1; j < m; j += 4) {
  17. print A, B, d[j], d[j + 1], d[j + 2], d[j + 3], d[m]
  18. }
  19. }
  20. }
复制代码

TOP

返回列表