aa2zz6

Clean text file

3 posts in this topic

#1 ·  Posted (edited)

Is there a way to clean this file up and only capture what's being displayed inside the parenthesis: EFV, EFC, Valve, Station, Regulator, etc (Material wording changes depending on input data from our operators)

I tried using the _ReplaceString function but it didn't work as expected. Certain things outside of the parenthesis were not being deleted or causing thing inside the parenthesis to be removed.

#include <file.au3>
_ReplaceStringInFile (@ScriptDir & "\Text.txt", "WordText", " ")
ClipPut (FileRead (@ScriptDir & "\text.txt"))

 

{"ParsedResults":[{"TextOverlay":{"Lines":[{"Words":[{"WordText":"EFV","Left":3.0,"Top":1.0,"Height":13.0,"Width":55.0},{"WordText":"EFC","Left":63.0,"Top":2.0,"Height":9.0,"Width":12.0},{"WordText":"Tap Tee","Left":81.0,"Top":1.0,"Height":10.0,"Width":49.0},{"WordText":"Valve","Left":135.0,"Top":1.0,"Height":10.0,"Width":51.0},{"WordText":"Station.","Left":192.0,"Top":1.0,"Height":10.0,"Width":18.0},{"WordText":"Regulator","Left":216.0,"Top":1.0,"Height":10.0,"Width":36.0},{"WordText":"Riser","Left":258.0,"Top":4.0,"Height":10.0,"Width":15.0},{"WordText":"Meter","Left":278.0,"Top":1.0,"Height":10.0,"Width":34.0},{"WordText":"Serviceline","Left":318.0,"Top":4.0,"Height":10.0,"Width":15.0},{"WordText":"Mainline","Left":338.0,"Top":4.0,"Height":7.0,"Width":14.0},{"WordText":"built","Left":358.0,"Top":1.0,"Height":10.0,"Width":28.0},{"WordText":"match.","Left":391.0,"Top":1.0,"Height":10.0,"Width":43.0},{"WordText":"Metersetting","Left":638.0,"Top":2.0,"Height":9.0,"Width":20.0}],"MaxHeight":13.0,"MinTop":1.0}],"HasOverlay":true,"Message":"Total lines: 1"},"FileParseExitCode":1,"ParsedText":"EFV EFC TapTee Valve Station\r\n","ErrorMessage":"","ErrorDetails":""}],"ExitCode":1,"IsErroredOnProcessing":false,"ErrorMessage":null,"ErrorDetails":null,"ProcessingTimeInMilliseconds":"1359"}

Trying to get something like this:

EFC
Valve
Regulator
Meter
Mainline
Metersetting

 

Edited by aa2zz6
Forgot to add something :|

Share this post


Link to post
Share on other sites



#2 ·  Posted

You can rewrite the code in the following example to get the data you want:

#NoTrayIcon

#include <Array.au3>
#include "JSON.au3"

Global $iJSON = '{"ParsedResults":[{"TextOverlay":{"Lines":[{"Words":[{"WordText":"EFV","Left":3.0,"Top":1.0,"Height":13.0,"Width":55.0},{"WordText":"EFC","Left":63.0,"Top":2.0,"Height":9.0,"Width":12.0},{"WordText":"Tap Tee","Left":81.0,"Top":1.0,"Height":10.0,"Width":49.0},{"WordText":"Valve","Left":135.0,"Top":1.0,"Height":10.0,"Width":51.0},{"WordText":"Station.","Left":192.0,"Top":1.0,"Height":10.0,"Width":18.0},{"WordText":"Regulator","Left":216.0,"Top":1.0,"Height":10.0,"Width":36.0},{"WordText":"Riser","Left":258.0,"Top":4.0,"Height":10.0,"Width":15.0},{"WordText":"Meter","Left":278.0,"Top":1.0,"Height":10.0,"Width":34.0},{"WordText":"Serviceline","Left":318.0,"Top":4.0,"Height":10.0,"Width":15.0},{"WordText":"Mainline","Left":338.0,"Top":4.0,"Height":7.0,"Width":14.0},{"WordText":"built","Left":358.0,"Top":1.0,"Height":10.0,"Width":28.0},{"WordText":"match.","Left":391.0,"Top":1.0,"Height":10.0,"Width":43.0},{"WordText":"Metersetting","Left":638.0,"Top":2.0,"Height":9.0,"Width":20.0}],"MaxHeight":13.0,"MinTop":1.0}],"HasOverlay":true,"Message":"Total lines: 1"},"FileParseExitCode":1,"ParsedText":"EFV EFC TapTee Valve Station\r\n","ErrorMessage":"","ErrorDetails":""}],"ExitCode":1,"IsErroredOnProcessing":false,"ErrorMessage":null,"ErrorDetails":null,"ProcessingTimeInMilliseconds":"1359"}'

Local $t, $sBreak
If StringInStr($iJSON, ",") Then
    $iJSON = StringRegExpReplace($iJSON, "[\[\]{}]", "")
    $sBreak = StringSplit($iJSON, ",")
    For $a = 1 To $sBreak[0]
        $t = _JSONDecode("{" & $sBreak[$a] & "}")
        _ArrayDisplay($t, "multi " & $a & " of " & $sBreak[0])
    Next
Else
    $t = _JSONDecode($iJSON)
    _ArrayDisplay($t, "single")
EndIf

 

JSON UDF HERE:

 


Regards,
 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

For such a simple work, a little regex does the trick  :)

$txt = FileRead("1.txt")

Local $res, $a = StringRegExp($txt, 'WordText":"([^"]+)', 3)
For $i = 0 to UBound($a)-1
  $res &= $a[$i] & @crlf
Next
Msgbox(0,"", $res)

 

Edited by mikell
2 people like this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now