标题: [文本处理] 【已解决】求助BAT代码中提取单个文本中几个关键词写到csv [打印本页]
作者: zhengwei007 时间: 2024-2-28 14:02 标题: 【已解决】求助BAT代码中提取单个文本中几个关键词写到csv
本帖最后由 zhengwei007 于 2024-2-28 23:38 编辑
代码如下,是以<skillTree>开头,</skillTree>结尾的多组文件,希望通过批处理对单个文件内的内容进行整理。- <!-- Human Fighter -->
- <skillTree type="classSkillTree" classId="0">
- <!-- Confirmed CT2.5 -->
- <skill skillName="Lucky" skillId="194" skillLvl="1" getLevel="1" autoGet="true" />
- <skill skillName="Expertise D" skillId="239" skillLvl="1" getLevel="20" autoGet="true" />
- </skillTree>
- <!-- Doomcryer -->
- <skillTree type="classSkillTree" classId="116" parentClassId="52">
- <!-- Confirmed CT2.5 and Updated to H5 -->
- <skill skillName="Wisdom" skillId="328" skillLvl="1" getLevel="76" levelUpSp="8300000" learnedByNpc="true" />
- <skill skillName="Health" skillId="329" skillLvl="1" getLevel="76" levelUpSp="8300000" learnedByNpc="true" />
- <skill skillName="Sigil Mastery" skillId="935" skillLvl="1" getLevel="76" levelUpSp="8300000" learnedByNpc="true" />
- </skillTree>
- <!-- Silver Ranger -->
- <skillTree type="classSkillTree" classId="24" parentClassId="22">
- <!-- Confirmed CT2.5 -->
- <skill skillName="Divine Inspiration" skillId="1405" skillLvl="1" getLevel="52" learnedByNpc="true">
- <item id="8618" count="1" />
- </skill>
- <skill skillName="Divine Inspiration" skillId="1405" skillLvl="2" getLevel="61" learnedByNpc="true">
- <item id="8619" count="1" />
- </skill>
复制代码
结果中,首行显示的是字段名,无需输出,我手填写就行。所有内容我自己粘到一个res.txt中,输出到sour.csv中即可。处理后的内容如下:- ClassId skillname skillId skillLvl getLevel levelUpSp Item ID item_count
- 0 luck 194 1 1
- 0 Expertise D 239 1 20
- 116 Wisdom 328 1 76 8300000
- 116 Health 329 1 76 8300000
- 116 Sigil Mastery 935 1 76 8300000
- 24 Divine Inspiration 1405 1 52 8618 1
- 24 Divine Inspiration 1405 2 61 8619 1
复制代码
谢谢各位大佬。
作者: 77七 时间: 2024-2-28 19:25
- <skill skillName="Lucky" skillId="194" skillLvl="1" getLevel="1" autoGet="true" />
- <skill skillName="Wisdom" skillId="328" skillLvl="1" getLevel="76" levelUpSp="8300000" learnedByNpc="true" />
- <skill skillName="Divine Inspiration" skillId="1405" skillLvl="1" getLevel="52" learnedByNpc="true">
复制代码
是否skillName 行只有这三种组合形式?
执行以下代码观察下
- find "skill skillName=" *.xml >1.txt
复制代码
作者: czjt1234 时间: 2024-2-28 21:32
- rem 另存为 ANSI 编码 的 bat
- ' & cls & CScript.exe /nologo /e:vbscript "%~f0" >sour.csv & pause & exit
-
- Option Explicit
- Dim oFSO, oDOMDocument, s
-
- Set oFSO = CreateObject("Scripting.FileSystemObject")
- Set oDOMDocument = CreateObject("Msxml2.DOMDocument.6.0")
- wsh.Echo "ClassId skillname skillId skillLvl getLevel levelUpSp Item ID item_count"
- For Each s In oFSO.GetFolder(oFSO.GetAbsolutePathName(".")).Files
- If LCase(oFSO.GetExtensionName(s)) = "xml" Then Call k(s.Path)
- Next
-
- Sub k(ByVal s)
- Dim i, j, m, t
- oDOMDocument.Load s
- For Each i In oDOMDocument.SelectNodes("//skillTree")
- For Each j In i.SelectNodes("skill")
- t = i.getAttribute("classId") & vbTab
- t = t & j.getAttribute("skillName") & vbTab _
- & j.getAttribute("skillId") & vbTab _
- & j.getAttribute("skillLvl") & vbTab _
- & j.getAttribute("getLevel") & vbTab _
- & j.getAttribute("levelUpSp") & vbTab
- For Each m In j.SelectNodes("item")
- t = t & m.getAttribute("id") & vbTab _
- & m.getAttribute("count") & vbTab
- Next
- wsh.Echo Left(t, Len(t) - 1)
- Next
- Next
- End Sub
复制代码
作者: ppll2030 时间: 2024-2-28 22:50
根据要求,只针对同级位置的res.txt文件处理,结果不要求显示标题行,并导出为sour.csv的表格中。- @echo off&setlocal enabledelayedexpansion
- for /f tokens^=1-12^delims^=^=^" %%0 in ('findstr "=" "res.txt"') do (
- if "%%2" == " classId" (set v1=%%3
- ) else (
- if "%%8" == " autoGet" (echo.!v1!, %%1, %%3, %%5, %%7
- ) else (
- if "%%8" == " learnedByNpc" (set v2=!v1!, %%1, %%3, %%5, %%7
- ) else (
- if "%%2" == " count" (echo !v2!, %%1, %%3
- ) else (
- echo.!v1!, %%1, %%3, %%5, %%7, %%9
- )
- )
- )
- )
- )>>sour.csv
复制代码
作者: zhengwei007 时间: 2024-2-28 23:37
czjt1234 发表于 2024-2-28 21:32
你们实在太厉害了。谢谢,可以用,太效率了。
作者: aloha20200628 时间: 2024-2-29 09:57
楼主的几轮帖子把纯P的for/f切割器开出刃来了...
- @echo off &setlocal enabledelayedexpansion
- (for /f tokens^=1-10^ delims^=^ ^=^<^" %%a in (' findstr "=" "1.xml" ') do (
- if /i "%%a"=="skillTree type" (set "v1=%%d")
- if /i "%%a"=="skill skillName" (
- if defined v (echo,)
- set "v=!v1! %%b %%d %%f %%h"
- if /i "%%j" neq "true" (set "v=!v! %%j")
- set/p="!v!"<nul)
- if /i "%%a"=="item id" (echo, %%b %%d&set "v=")
- ))>"sour.csv"
- endlocal&exit/b
复制代码
作者: aloha20200628 时间: 2024-2-29 10:44
再换一种纯P老刀的切法,顺便复习一遍 if 句式中如何比较双引号"的方法...- @echo off &setlocal enabledelayedexpansion
- (for /f "tokens=1* delims= < " %%a in (' findstr "=" "1.xml" ') do (
- if /i "%%~a"=="skillTree" for /f tokens^=1-4^ delims^=^" %%1 in ("%%~b") do (set "v1=%%4")
- if /i "%%~a"=="skill" (
- if defined v (echo,)
- set "v=!v1!" & for %%i in (%%~b) do (
- set i=%%i &set "c=!i:~,1!"
- if ^!c!==^" if /i "!i:~1,4!" neq "true" set "v=!v! !i:~1,-2!")
- set/p="!v!"<nul
- )
- if /i "%%~a"=="item" for /f tokens^=1-4^ delims^=^" %%1 in ("%%~b") do (echo, %%2 %%4&set "v=")
- ))>"sour.csv"
- endlocal&exit/b
复制代码
作者: qixiaobin0715 时间: 2024-2-29 15:25
本帖最后由 qixiaobin0715 于 2024-2-29 16:36 编辑
先使用for /f中的切分功能排除干扰字符,后面直指主题,即直接找出各字段对应的内容,感觉这样应当更准确些,防止出现某些位置异常而产生匹配错误的情况:- @echo off
- set /a #ClassId=#skillname=#skillId=#skillLvl=#getLevel=#levelUpSp=#ID=#count=0
- setlocal enabledelayedexpansion
- for /f "delims=<> " %%x in (1.log) do (
- for /f "tokens=1*" %%i in ("%%x") do (
- for %%a in (%%j) do (
- if defined x (
- set !x!=%%~a
- set x=
- ) else if defined #%%a (
- set x=%%a
- )
- if /i "%%a"=="/" (
- echo,!ClassId! !skillname! !skillId! !skillLvl! !getLevel! !levelUpSp! !ID! !count!
- for %%y in (skillname skillId skillLvl getLevel levelUpSp ID count) do set %%y=
- )
- )
- )
- )
- pause
复制代码
开始上面代码好像有些问题,现已修改。
作者: aloha20200628 时间: 2024-2-29 16:14
用jscript在纯P壳里跑一遍,处理大数据应会有明显效率差别
以下代码存为test.bat运行,结果生成与源文件 1.xml (一楼示例样本)对应的 sour.csv- @set @v=1 /* @echo off & type "1.xml"|cscript /e:jscript "%~f0">"sour.csv" &exit/b
- */
- var lines = WSH.stdin.readall().split('\n');
- var v1, v, tk, tk0, ltk, vLines = [];
- for (var n=0,l=lines.length; n<l; n++) {
- if (lines[n].indexOf('=') == -1) continue;
- tk = lines[n].split(/[\t=<]+/); ltk = tk.length; tk0 = tk[0].toLowerCase();
- if (tk0 == 'skilltree type') v1 = tk[2].split(/"+/)[0];
- else if (tk0 == 'skill skillname') {
- v = v1;
- for (var k=1; k<ltk; k++)
- if (tk[k].slice(0,1) == '\"' && tk[k].slice(1,5).toLowerCase() != 'true')
- v += '\t'+tk[k].split(/"+/)[0];
- vLines.push(v);
- } else if (tk0 == 'item id') {
- v += '\t'+tk[1].split(/"+/)[0]+'\t'+tk[2].split(/"+/)[0]; vLines.pop(); vLines.push(v);
- }
- }
- WSH.echo(vLines.join('\n'));
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |