我需要并搜索结果以将我的.txt文件转换为具有该格式的.json文件:
{
"id":"12777475756802056",
"typ":"Solo",
"match":
{
"rank":"1",
"playeruserid":"165496871657",
"playername":"Example Name",
"kills":"8",
"points":"224000",
"killer":"empty", // while firstplace
"weapon":"empty" // while firstplace
},
{
"rank":"2",
"playeruserid":"654987654984",
"playername":"Example Name 2",
"kills":"4",
"points":"168000",
"killer":"Example Name",
"weapon":"Shotgun"
}
... another players here.
}
我的.txt文件看起来如此,是的非常糟糕......有这么多空格,所有这些都在一行...:https://pastebin.com/FP0C9BCj
我尝试这个批次删除了许多空格:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=D:\Users\Raphael\Desktop"
SET "destdir=D:\Users\Raphael\Desktop"
SET "filename1=%sourcedir%\matchreport.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
SET "line=%%a"
SET "line=!line:?=!"
SET "line=!line:/=-!"
SET "line=!line::=!"
SET "line=!line: =;!"
ECHO !line!
)
)>"%outfile%"
GOTO :EOF
和idk现在为什么现在可以将它转换为json文件,而一行专业播放器和json格式的所有信息现在都是:
{"1. 5605****11014 Cl***ma 205000 + 19000 ( 8) = 224000"}
抱歉这是我第一次尝试将txt转换为json文件你有没有想过转换它? :/
答案 0 :(得分:2)
这几乎可行。 (需要PowerShell 3.0或更高版本,因此,Windows 8或更高版本。)这是批处理+ PowerShell多语言。用.bat扩展名保存。
<# : batch portion
@echo off & setlocal
set "file=test.txt"
powershell -noprofile "iex (${%~f0} | out-string)"
exit /b
: end batch / begin powershell #>
function Parse-Fields() {
add-type -as Microsoft.VisualBasic
$parser = new-object Microsoft.VisualBasic.FileIO.TextFieldParser($env:file)
$parser.TextFieldType = "FixedWidth"
$parser.TrimWhiteSpace = $true
$parser.FieldWidths = @(7, 21, 25, 6, 9, 4, 12, 26, -1)
while (!$parser.EndOfData) {
try {
$parser.ReadFields() -join "," -replace "[\(\+\)\.=]" | ?{ $_ -match "\d" }
}
catch {}
}
$parser.Close()
}
$header = "ID","GameUserId","Name","Rank","Kills","Score","Total","Killer","Weapon"
Parse-Fields | ConvertFrom-Csv -Header $header | ConvertTo-Json
输出:
[
{
"ID": "1",
"GameUserId": "5605****11014",
"Name": "Cl***ma",
"Rank": "205000",
"Kills": "19000",
"Score": "8",
"Total": "224000",
"Killer": "",
"Weapon": "m4"
},
{
"ID": "2",
"GameUserId": "238444****020",
"Name": "Ap*******ift",
"Rank": "172403",
"Kills": "11550",
"Score": "3",
"Total": "183953",
"Killer": "Cl***ma",
"Weapon": "m4"
},
{
"ID": "3",
"GameUserId": "92******9515",
"Name": "Sw****UK",
"Rank": "156259",
"Kills": "14900",
"Score": "6",
"Total": "171159",
"Killer": "Ap*******ift",
"Weapon": "m4"
},
{
"ID": "4",
"GameUserId": "6583833***132",
"Name": "Moc********kap",
"Rank": "144805",
"Kills": "2000",
"Score": "1",
"Total": "146805",
"Killer": "Cl***ma",
"Weapon": "shotgun"
},
{
"ID": "5",
"GameUserId": "621***7360388",
"Name": "Ol***r***",
"Rank": "135920",
"Kills": "6200",
"Score": "3",
"Total": "142120",
"Killer": "Ap*******ift",
"Weapon": "m4"
},
{
"ID": "6",
"GameUserId": "189661****980",
"Name": "Op*********gon",
"Rank": "128661",
"Kills": "0",
"Score": "0",
"Total": "128661",
"Killer": "Sw****UK",
"Weapon": "m4"
},
{
"ID": "7",
"GameUserId": "6408****79452",
"Name": "M********nner",
"Rank": "122523",
"Kills": "3500",
"Score": "2",
"Total": "126023",
"Killer": "Sw****UK",
"Weapon": "shotgun"
},
{
"ID": "8",
"GameUserId": "59060***2163",
"Name": "A***g",
"Rank": "117207",
"Kills": "0",
"Score": "0",
"Total": "117207",
"Killer": "Ap*******ift",
"Weapon": "ak"
},
{
"ID": "9",
"GameUserId": "831467****599",
"Name": "*********ngstar",
"Rank": "112517",
"Kills": "0",
"Score": "0",
"Total": "112517",
"Killer": "Ol***r***",
"Weapon": "shotgun"
},
{
"ID": "10",
"GameUserId": "34542****7961",
"Name": "********Really",
"Rank": "108322",
"Kills": "5000",
"Score": "3",
"Total": "113322",
"Killer": "[Toxic Gas]",
"Weapon": null
},
{
"ID": "11",
"GameUserId": "904****58750",
"Name": "******tch",
"Rank": "104528",
"Kills": "3500",
"Score": "2",
"Total": "108028",
"Killer": "Sw****UK",
"Weapon": "shotgun"
}
]
根据您的要求按摩它可能不会花费太多时间。我更喜欢将数据作为平面文本进行客观化。该脚本首先将数据视为固定宽度的CSV文件。一旦被客体化为CSV数据,它就被转换为JSON。如果没有其他人建议更好的答案,也许您可以将其用作构建项目的骨架。
作为替代方案,这里有一个Batch + JScript多语言,它比PowerShell方法更有效,尽管它更长。它不会受到TextFieldParser()
类所需的开销,而JScript通常比PowerShell更快。作为奖励,它应该适用于早期版本的Windows,只要安装IE 9或更新版本(我认为Vista SP2?)。如果你对JavaScript更加满意,你可能更喜欢这个。
@if (@CodeSection == @Batch) @then
@echo off & setlocal
set "file=test.txt"
cscript /nologo /e:JScript "%~f0" < "%file%"
exit /b
@end // end Batch / begin JScript
var stdin = WSH.CreateObject('Scripting.FileSystemObject').GetStandardStream(0),
file = stdin.ReadAll(),
htmlfile = WSH.CreateObject('htmlfile'),
csvfields = {"ID": 7, "GameUserId": 21, "Name": 25, "Rank": 6, "Kills": 9,
"Score": 4, "Total": 12, "Killer": 26, "Weapon": null};
String.prototype.clean = function() {
var val = this.replace(/[\(\)\.\+=]/g, '').replace(/^\s+|\s+$/g, '');
return /^\d+$/.test(val) ? val * 1 : val;
}
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
var JSON = htmlfile.parentWindow.JSON,
lines = obj = new htmlfile.parentWindow.Array();
htmlfile.close();
lines = file.split(/\r?\n/g);
lines.splice(0,4);
for (var i=0; i<lines.length; i++) {
for (var field in csvfields) {
if (!obj[i]) obj.push({});
obj[i][field] = csvfields[field] ?
lines[i].substring(0, csvfields[field]).clean() : lines[i].clean();
lines[i] = lines[i].substring(csvfields[field]);
}
}
WSH.Echo(JSON.stringify(obj, null, ' '));
输出类似,只是未加引号的整数值。
答案 1 :(得分:1)
仅仅是为了哎呀,我使用我的JREPL.BAT utility实现了一个正则表达式解决方案。
我认为星号是试图掩盖数据,并且它们不会出现在实际输入中。我假设ID严格来说是数字的。我还假设每个名称后面至少有两个空格(用于表示名称的结尾)。
以下是我使用JREPL.BAT的批处理脚本:
@echo off
setlocal
:: ID Type
:: 1 2 3
set "find1=^Match ID: *(\d+), *(.*?) *$"
:: Rank ID Name Kills Points Killer Weapon
:: 4 5 6 7 8 9 10 11
set "find2=^ *(\d+)\. +(\d+) +(.*\S) +\d+ \+ +\d+ \( *(\d+)\) = *(\d+) +(.*?\S) +($|.*\S)"
set "repl1={\n\qid\q:\q$2\q,\n\qtyp\q:\q$3\q,\n\qmatch\q:"
set "repl2= {\n"
set "repl2=%repl2% \qrank\q:\q$5\q,\n"
set "repl2=%repl2% \qplayeruserid\q:\q$6\q,\n"
set "repl2=%repl2% \qplayername\q:\q$7\q,\n"
set "repl2=%repl2% \qkills\q:\q$8\q,\n"
set "repl2=%repl2% \qpoints\q:\q$9\q,\n"
set "repl2=%repl2% \qkiller\q:\q$10\q,\n"
set "repl2=%repl2% \qweapon\q:\q$11\q\n"
set "repl2=%repl2% },"
set "find=%find1%/%find2%"
set "repl=%repl1%/%repl2%"
jrepl find repl /v /t "/" /x /a /f test.txt | jrepl "," "\n}" /x /inc -1
这是伪固定&#34; test.txt&#34;删除了星号的输入文件(数字或字母代替星号)。为了测试我的正则表达式,我故意为一些名字和武器添加了一个内部空间。
Match ID: 6549999999997461, Solo
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Rank GameUserId Name Rank Kills (##) Total Killer Weapon
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. 5605999911014 Claaama 205000 + 19000 ( 8) = 224000 m4
2. 2384449999020 Apaaaaaaaift 172403 + 11550 ( 3) = 183953 Claaama m4
3. 929999999515 Swaa aUK 156259 + 14900 ( 6) = 171159 Apaaaaaaaift m4
4. 6583833999132 Mocaaaaaaaakap 144805 + 2000 ( 1) = 146805 Claaama shotgun
5. 6219997360388 Olaaaraaa 135920 + 6200 ( 3) = 142120 Apaaaaaaaift m4
6. 1896619999980 Opaaaaaaaaagon 128661 + 0 ( 0) = 128661 SwaaaaUK m4
7. 6408999979452 Maaaa aaanner 122523 + 3500 ( 2) = 126023 Swaa aUK shot gun
8. 590609992163 Aaaag 117207 + 0 ( 0) = 117207 Apaaaaaaaift ak
9. 8314679999599 aaaaaaaaangstar 112517 + 0 ( 0) = 112517 Olaaaraaa shotgun
10. 3454299997961 aaaaaaaaReally 108322 + 5000 ( 3) = 113322 [Toxic Gas]
11. 9049999958750 aaaa atch 104528 + 3500 ( 2) = 108028 Swaa aUK shot gun
以下是最终结果:
{
"id":"6549999999997461",
"typ":"Solo",
"match":
{
"rank":"1",
"playeruserid":"5605999911014",
"playername":"Claaama",
"kills":"8",
"points":"224000",
"killer":"m4",
"weapon":""
},
{
"rank":"2",
"playeruserid":"2384449999020",
"playername":"Apaaaaaaaift",
"kills":"3",
"points":"183953",
"killer":"Claaama",
"weapon":"m4"
},
{
"rank":"3",
"playeruserid":"929999999515",
"playername":"Swaa aUK",
"kills":"6",
"points":"171159",
"killer":"Apaaaaaaaift",
"weapon":"m4"
},
{
"rank":"4",
"playeruserid":"6583833999132",
"playername":"Mocaaaaaaaakap",
"kills":"1",
"points":"146805",
"killer":"Claaama",
"weapon":"shotgun"
},
{
"rank":"5",
"playeruserid":"6219997360388",
"playername":"Olaaaraaa",
"kills":"3",
"points":"142120",
"killer":"Apaaaaaaaift",
"weapon":"m4"
},
{
"rank":"6",
"playeruserid":"1896619999980",
"playername":"Opaaaaaaaaagon",
"kills":"0",
"points":"128661",
"killer":"SwaaaaUK",
"weapon":"m4"
},
{
"rank":"7",
"playeruserid":"6408999979452",
"playername":"Maaaa aaanner",
"kills":"2",
"points":"126023",
"killer":"Swaa aUK",
"weapon":"shot gun"
},
{
"rank":"8",
"playeruserid":"590609992163",
"playername":"Aaaag",
"kills":"0",
"points":"117207",
"killer":"Apaaaaaaaift",
"weapon":"ak"
},
{
"rank":"9",
"playeruserid":"8314679999599",
"playername":"aaaaaaaaangstar",
"kills":"0",
"points":"112517",
"killer":"Olaaaraaa",
"weapon":"shotgun"
},
{
"rank":"10",
"playeruserid":"3454299997961",
"playername":"aaaaaaaaReally",
"kills":"3",
"points":"113322",
"killer":"[Toxic Gas]",
"weapon":""
},
{
"rank":"11",
"playeruserid":"9049999958750",
"playername":"aaaa atch",
"kills":"2",
"points":"108028",
"killer":"Swaa aUK",
"weapon":"shot gun"
}
}
答案 2 :(得分:0)
Xidel虽然主要是html / xml / json数据提取工具,但也可以用一点XPath / XQuery魔术来处理原始文本。
我正在使用dbenham伪固定的' test.txt '作为输入
Match ID: 6549999999997461, Solo
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Rank GameUserId Name Rank Kills (##) Total Killer Weapon
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. 5605999911014 Claaama 205000 + 19000 ( 8) = 224000 m4
2. 2384449999020 Apaaaaaaaift 172403 + 11550 ( 3) = 183953 Claaama m4
3. 929999999515 Swaa aUK 156259 + 14900 ( 6) = 171159 Apaaaaaaaift m4
4. 6583833999132 Mocaaaaaaaakap 144805 + 2000 ( 1) = 146805 Claaama shotgun
5. 6219997360388 Olaaaraaa 135920 + 6200 ( 3) = 142120 Apaaaaaaaift m4
6. 1896619999980 Opaaaaaaaaagon 128661 + 0 ( 0) = 128661 SwaaaaUK m4
7. 6408999979452 Maaaa aaanner 122523 + 3500 ( 2) = 126023 Swaa aUK shot gun
8. 590609992163 Aaaag 117207 + 0 ( 0) = 117207 Apaaaaaaaift ak
9. 8314679999599 aaaaaaaaangstar 112517 + 0 ( 0) = 112517 Olaaaraaa shotgun
10. 3454299997961 aaaaaaaaReally 108322 + 5000 ( 3) = 113322 [Toxic Gas]
11. 9049999958750 aaaa atch 104528 + 3500 ( 2) = 108028 Swaa aUK shot gun
xidel.exe -s test.txt -e "let $line:=tokenize($raw,'\r\n'),$match:=extract($line[1],'^Match ID: (\d+), (.+)',(1,2)) return {'id':$match[1],'type':$match[2],'match':$line[position()>4] ! (let $obj:=extract(.,'^ *(\d+)\. +(\d+) +(.*?\S) .+\( ?(\d+)\) = (\d+) {3}(.*?\S?.*?)? +($|.*\S)',1 to 7) return {'rank':$obj[1],'playeruserid':$obj[2],'playername':$obj[3],'kills':$obj[4],'points':$obj[5],'killer':$obj[6],'weapon':$obj[7]})}"
或更具可读性(打印精美):
xidel.exe -s test.txt -e ^" ^
let $line:=tokenize( ^
$raw, ^
'\r\n' ^
), ^
$match:=extract( ^
$line[1], ^
'Match ID: (\d+), (.+)', ^
(1,2) ^
) ^
return { ^
'id':$match[1], ^
'type':$match[2], ^
'match':$line[position()^>4] ! ( ^
let $obj:=extract( ^
., ^
'^^ *(\d+)\. +(\d+) +(.*?\S) .+\( ?(\d+)\) = (\d+) {3}(.*?\S?.*?)? +($^|.*\S)', ^
1 to 7 ^
) return { ^
'rank':$obj[1], ^
'playeruserid':$obj[2], ^
'playername':$obj[3], ^
'kills':$obj[4], ^
'points':$obj[5], ^
'killer':$obj[6], ^
'weapon':$obj[7] ^
} ^
) ^
}^"
(每行的结尾以及"
,>
,^
,|
都必须用^
进行转义)>
打开' test.txt '之后,我们首先创建2个内部变量; $line
和$match
。
$line
变成一系列单独的行。通过从输入的第一行中提取$match
,它会变成匹配ID和类型的序列。
分配这两个内部变量后,我们开始创建json。 id
属性获取匹配ID为值,type
属性获取“ Solo”。
接下来,我们创建match
数组并开始处理第5行及以上。我们输入extract()
函数的每一行以创建另一个内部变量(dbenham的正则表达式略有更改)。此变量包含我们要查找的所有单个值的序列(7个项目)。并使用此变量最终填充对象,在其中将rank
属性分配给第一项,将playeruserid
属性分配给第二项,依此类推。
输出:
{
"id": "6549999999997461",
"type": "Solo",
"match": [
{
"rank": "1",
"playeruserid": "5605999911014",
"playername": "Claaama",
"kills": "8",
"points": "224000",
"killer": "",
"weapon": "m4"
},
{
"rank": "2",
"playeruserid": "2384449999020",
"playername": "Apaaaaaaaift",
"kills": "3",
"points": "183953",
"killer": "Claaama",
"weapon": "m4"
},
{
"rank": "3",
"playeruserid": "929999999515",
"playername": "SwaaaaUK",
"kills": "6",
"points": "171159",
"killer": "Apaaaaaaaift",
"weapon": "m4"
},
{
"rank": "4",
"playeruserid": "6583833999132",
"playername": "Mocaaaaaaaakap",
"kills": "1",
"points": "146805",
"killer": "Claaama",
"weapon": "shotgun"
},
{
"rank": "5",
"playeruserid": "6219997360388",
"playername": "Olaaaraaa",
"kills": "3",
"points": "142120",
"killer": "Apaaaaaaaift",
"weapon": "m4"
},
{
"rank": "6",
"playeruserid": "1896619999980",
"playername": "Opaaaaaaaaagon",
"kills": "0",
"points": "128661",
"killer": "SwaaaaUK",
"weapon": "m4"
},
{
"rank": "7",
"playeruserid": "6408999979452",
"playername": "Maaaaaaaanner",
"kills": "2",
"points": "126023",
"killer": "SwaaaaUK",
"weapon": "shotgun"
},
{
"rank": "8",
"playeruserid": "590609992163",
"playername": "Aaaag",
"kills": "0",
"points": "117207",
"killer": "Apaaaaaaaift",
"weapon": "ak"
},
{
"rank": "9",
"playeruserid": "8314679999599",
"playername": "aaaaaaaaangstar",
"kills": "0",
"points": "112517",
"killer": "Olaaaraaa",
"weapon": "shotgun"
},
{
"rank": "10",
"playeruserid": "3454299997961",
"playername": "aaaaaaaaReally",
"kills": "3",
"points": "113322",
"killer": "[Toxic Gas]",
"weapon": ""
},
{
"rank": "11",
"playeruserid": "9049999958750",
"playername": "aaaaaatch",
"kills": "2",
"points": "108028",
"killer": "SwaaaaUK",
"weapon": "shotgun"
}
]
}