标题: [文本处理] 【已解决】求助从CSV中读取内容后重新整理成一行行的 [打印本页]
作者: zhengwei007 时间: 2024-3-14 00:32 标题: 【已解决】求助从CSV中读取内容后重新整理成一行行的
本帖最后由 zhengwei007 于 2024-3-15 09:42 编辑
我有一个rec.csv文件,里面内容如下:- 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}}
- 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}}
- 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组数字的最后一组,写到前面的最后一组给补齐即可。
通过整理希望达到如下效果:- 3 corpse_make_list charcoal 1 1 5.6617
- 3 corpse_make_list magic_ring 1 1 34.3131
- 3 corpse_make_list rp_broad_sword 1 1 4.5293
- 3 additional_make_multi_list adena 30 42 100 70
- 3 additional_make_multi_list apprentice's_earing 1 1 30.9859 25.7009
- 3 additional_make_multi_list necklace_of_magic 1 1 23.0047 25.7009
- 3 additional_make_multi_list magic_ring 1 1 46.0094 25.7009
- 3 additional_make_multi_list stem 1 1 29.1262 6.6988
- 3 additional_make_multi_list varnish 1 1 14.5631 6.6988
- 3 additional_make_multi_list suede 1 1 9.7087 6.6988
- 3 additional_make_multi_list silver_nugget 1 1 5.8252 6.6988
- 3 additional_make_multi_list thread 1 1 29.1262 6.6988
- 3 additional_make_multi_list rp_bow 1 1 11.6506 6.6988
- 4 corpse_make_list suede 1 1 5.1827
- 4 corpse_make_list willow_staff_head 1 1 0.5553
- 4 corpse_make_list broad_sword_blade 1 1 0.5553
- 4 additional_make_multi_list adena 41 58 100 70
- 4 additional_make_multi_list cotton_shoes 1 1 33.3333 1.5132
- 4 additional_make_multi_list crude_leather_shoes 1 1 33.3333 1.5132
- 4 additional_make_multi_list short_leather_gloves 1 1 33.3334 1.5132
- 4 additional_make_multi_list charcoal 1 1 50 3.0618
- 4 additional_make_multi_list suede 1 1 33.3333 3.0618
- 4 additional_make_multi_list rp_wooden_arrow 1 1 16.6667 3.0618
- 5 corpse_make_list lesser_healing_potion 1 1 21.6259
- 5 corpse_make_list suede 1 1 6.4878
- 5 corpse_make_list rp_wooden_arrow 1 1 3.2439
- 5 additional_make_multi_list adena 51 72 100 70
- 5 additional_make_multi_list small_shield 1 1 58.8121 1.5322
- 5 additional_make_multi_list leather_cap 1 1 41.1879 1.5322
- 5 additional_make_multi_list coal 1 1 13.9182 10.3267
- 5 additional_make_multi_list varnish 1 1 13.9182 10.3267
- 5 additional_make_multi_list rp_cedar_staff 1 1 2.5727 10.3267
- 5 additional_make_multi_list wooden_arrow 10 30 69.5909 10.3267
复制代码
我希望通过批处理,执行后自动l输出到sour.csv中即可。
作者: czjt1234 时间: 2024-3-14 06:57
为什么前三行是三组数字?
作者: zhengwei007 时间: 2024-3-14 08:39
本帖最后由 zhengwei007 于 2024-3-14 16:30 编辑
为什么前三行是三组数字?
czjt1234 发表于 2024-3-14 06:57
自身的格式就是这样,所以根据前面的英文,corpse_make_list这个类型就是3组数字,整理出来后,这个一行是5个字段,而additional这个开头的,是6个字段。
作者: aloha20200628 时间: 2024-3-14 22:06
回复 1# zhengwei007
再用纯P版来一轮“庖丁解牛”...
用一楼示例测试通过- @echo off &setlocal enabledelayedexpansion
- (for /f "tokens=1-5 delims== " %%1 in (rec.csv) do (
- set "vc=%%3"&set "vc=!vc:};{=,!"&set "vc=!vc:;=/!"
- for %%c in ( {,[,],} ) do (set "vc=!vc:%%c=!")
- for %%e in (!vc!) do (set "_vc=%%e"&echo,%%1 %%2 !_vc:/= !)
- set "va=%%5"&set "va=!va:~5!"&set "va=!va:{{[=,!"&set "va=!va:;=/!"
- for %%a in (!va!) do (
- set "av=%%a"&set "vn=!av:*}}/=!"&for %%c in ( {,/,} ) do (set "vn=!vn:%%c=!")
- for %%q in (!vn!) do (set "_av=!av:/%%q=!")
- set "_av=!_av:}/{=,!"
- for %%c in ( {,[,],} ) do (set "_av=!_av:%%c=!")
- for %%e in (!_av!) do (set "v=%%e"&echo,%%1 %%4 !v:/= ! !vn!)
- )
- ))>"sour.csv"
- endlocal&exit/b
复制代码
作者: ppll2030 时间: 2024-3-14 23:08
一楼测试通过。- @echo off
- setlocal enabledelayedexpansion
- (for /f "delims=" %%a in (res.csv) do (call :str1 "%%a"))>a.log
- (for /f "delims=" %%a in (a.log) do (call :str2 "%%a"))>b.log
- (for /f "tokens=1* delims==" %%i in (b.log) do (if "%%j" neq "" (echo %%i& echo %%j) else echo %%i))>a.log
- for /f "tokens=1-7* delims=[]{}; " %%1 in (a.log) do (
- ::echo %%1 %%2 %%3 %%4 %%5
- if "%%2" == "" (echo %%1|findstr /v "[^0-9]" >nul&&set v1=%%1||set v2=%%1
- ) else (
- if "!v2!" == "corpse_make_list" (echo !v1!, !v2!, %%1, %%2, %%3, %%4
- ) else (
- if "%%5" == "" (
- echo m!n!=!v1!, !v2!, %%1, %%2, %%3, %%4 >>$
- set /a n+=1
- ) else (
- for /f "tokens=2 delims==" %%a in ('type "$"') do echo %%a, %%5
- echo !v1!, !v2!, %%1, %%2, %%3, %%4, %%5
- del /q $ 2>nul
- )
- )
- )
- )>>sour.csv
- del /q a.log b.log
- exit
-
- :str1
- set "str=%~1"
- echo;%str: =&echo %
- goto:eof
- :str2
- set "str=%~1"
- echo;%str:};{=&echo %
- goto:eof
复制代码
作者: WHY 时间: 2024-3-15 00:32
本帖最后由 WHY 于 2024-3-15 17:06 编辑
- @if(0)==(0) echo off
- type 1.txt | cscript -nologo -e:jscript "%~f0" > 2.txt
- pause & exit
- @end
-
- var reg = /\[[^[\]]+\](?:;\d+(?:\.\d+)?){3}\}*(;\d+(?:\.\d+)?)?/g;
-
- while (!WSH.StdIn.AtEndOfStream) {
- var arr = WSH.StdIn.ReadLine().split('\t');
- var s0 = arr[0]; //第1列
- for (var i=1; i<arr.length; i++) {
- var s1 = arr[i].split('=')[0]; //第2列
- var ret = [];
- while (m = reg.exec(arr[i])) {
- var s2 = m[0].replace(/[[\]}]/g, '').replace(/;/g, '\t'); //第3-6列
- s2 = s0 + '\t' + s1 + '\t' + s2;
- ret.push(s2);
- if (m[1] != '') {
- var s4 = m[1].replace(';', '\t'); //第4个数字列
- for (var j=0; j<ret.length-1; j++) ret[j] += s4;
- WSH.Echo(ret.join('\r\n'));
- ret = [];
- }
- }
- if (ret.length) WSH.Echo(ret.join('\r\n'));
- }
- }
复制代码
作者: zhengwei007 时间: 2024-3-15 09:42
谢谢楼上几位,搞定啦
作者: hfxiang 时间: 2024-3-15 09:53
回复 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即可:- BEGIN {
- OFS = "\t"
- }
- {
- A = $1
- B = gensub(/^(.+)=.+$/, "\\1", "1", $2)
- C = gensub(/^[^]\[\]]+(({\[[^]\[\]]+\];[0-9]+;[0-9]+;[0-9]+(\.[0-9]+)?};?)+)[^]\[\]]+$/, "\\1", "1", $2)
- n = patsplit(C, c, /[^\[\];\{\}]+/)
- for (i = 1; i <= n; i += 4) {
- print A, B, c[i], c[i + 1], c[i + 2], c[i + 3] OFS
- }
- B = gensub(/^(.+)=.+$/, "\\1", "1", $3)
- n = patsplit($3, c, /{(({\[[^]\[\]]+\];[0-9]+;[0-9]+;[0-9]+(\.[0-9]+)?};?)+)};[0-9]+(\.[0-9]+)?/)
- for (i = 1; i <= n; i++) {
- m = patsplit(c[i], d, /[^\[\];\{\}]+/)
- for (j = 1; j < m; j += 4) {
- print A, B, d[j], d[j + 1], d[j + 2], d[j + 3], d[m]
- }
- }
- }
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |