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

[文本处理] 【已解决】求助批处理从一堆文本文件中提取关键词写到csv

本帖最后由 zhengwei007 于 2024-2-27 13:25 编辑

我想从100多个文档里面,发现有Soul Crystal字符时,截取<item>这段内容的字段。字段包括每个ID和count数量,单独存放在csv文件里。
文档中是以<item>开头,</item>结尾的,谢谢大家。
由于不能上传附件,我粘点里面内容吧。
  1. <item>
  2. <!-- Stormbringer -->
  3. <ingredient count="1" id="72" />
  4. <!-- Red Soul Crystal - Stage 5 -->
  5. <ingredient count="1" id="4634" />
  6. <!-- Gemstone C -->
  7. <ingredient count="97" id="2131" />
  8. <!-- Adena -->
  9. <ingredient count="291000" id="57" />
  10. <!-- Stormbringer - Critical Anger -->
  11. <production count="1" id="4681" />
  12. </item>
  13. <item>
  14. <!-- Guardian Sword - Great Gale -->
  15. <ingredient count="1" id="10955" />
  16. <!-- Adena -->
  17. <ingredient count="3390000" id="57" />
  18. <!-- Gemstone B -->
  19. <ingredient count="339" id="2132" />
  20. <!-- Blue Soul Crystal - Stage 10 -->
  21. <ingredient count="1" id="4661" />
  22. <!-- Guardian Sword - Great Gale - Critical Bleed -->
  23. <production count="1" id="10958" />
  24. </item>
  25. <item>
  26. <!-- Blood Brother - Great Gale -->
  27. <ingredient count="1" id="21966" />
  28. <!-- Blue Soul Crystal - Stage 14 -->
  29. <ingredient count="1" id="9571" />
  30. <!-- Gemstone S -->
  31. <ingredient count="285" id="2134" />
  32. <!-- Blood Brother - Great Gale - Light -->
  33. <production count="1" id="21969" />
  34. </item>
复制代码
每个文件里可能有多个这样的内容结构。
最终csv就是这样:
72        1        4634        1        2131        97        57        291000        4681        1
10955        1        57        3390000        2132        339        4661        1        10958        1
21966        1        9571        1        2134        285        21969        1               
谢谢各位大哥。

回复 1# zhengwei007


请用使用网盘上传原始文件。

如果需要上传截图,可以找个图床,例如:
http://bbs.bathome.net/thread-60985-1-1.html
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 1# zhengwei007

题型类同(http://www.bathome.net/thread-68463-1-1.html)中6楼解法...
先匹配当前目录中所有包含‘Soul Crystal’字段的*.xml文件,再逐一提取目标数据,结果生成与之对应的*.csv
假设源文件*.xml均为utf-8编码,若是ansi或简中编码(gb2312/gbk),则删除第2行代码即可
  1. @echo off &setlocal enabledelayedexpansion
  2. chcp 65001>nul
  3. for /f "delims=" %%F in (' findstr /imc:"Soul Crystal" *.xml') do (
  4. (for /f tokens^=1-4^delims^=^" %%1 in (' findstr "=" "%%~F" ') do for /f "tokens=1 delims= < " %%a in ("%%~1") do if /i "%%~a"=="production" (echo,!v:~1! %%4 %%2&set "v=") else (set "v=!v! %%4 %%2")
  5. )>"%%~F.csv"
  6. )
  7. endlocal&exit/b
复制代码

TOP

谢谢楼上几位,已经解决问题。

TOP

返回列表