Sign in to follow this  
Followers 0
stefano

Parsing data in a text file

15 posts in this topic

I have a text file which is currently filled with a list of albums/artists:

<artist>AC DC</artist><album>Back in Black</album>
<artist>The Beatles</artist><album>Sgt. Pepper's Lonely Hearts Cl</album>
<artist>The Beatles</artist><album>Abbey Road</album>
<artist>Bob Dylan</artist><album>Great Woods 1993-09-13</album>
<artist>Bumblefoot</artist><album>Hands</album>
<artist>Buon Vecchio Charlie</artist><album>1971 - Buon Vecchio Charlie</album>
<artist>Camel</artist><album>Mirage</album>

I want to be able to parse the data in between the <artist> or <album> tags into a stored variable but I am having no luck so far.

Can anyone recomend a method of doing so?

The layout of the text file is adjustable, it does not have to be inbetween <artist> etc tags.

Share this post


Link to post
Share on other sites



I have a text file which is currently filled with a list of albums/artists:

<artist>AC DC</artist><album>Back in Black</album>
<artist>The Beatles</artist><album>Sgt. Pepper's Lonely Hearts Cl</album>
<artist>The Beatles</artist><album>Abbey Road</album>
<artist>Bob Dylan</artist><album>Great Woods 1993-09-13</album>
<artist>Bumblefoot</artist><album>Hands</album>
<artist>Buon Vecchio Charlie</artist><album>1971 - Buon Vecchio Charlie</album>
<artist>Camel</artist><album>Mirage</album>

I want to be able to parse the data in between the <artist> or <album> tags into a stored variable but I am having no luck so far.

Can anyone recomend a method of doing so?

The layout of the text file is adjustable, it does not have to be inbetween <artist> etc tags.

You could do a stringSplit on the '<' sign then a "stringtrimleft" 7 characters

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Can anyone recomend a method of doing so?

StringRegExp() or search the forum for "+Microsoft +XMLDOM" if the file has a real XML structure.

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Hi,

what about

Func _StringBetween1($s_String, $s_Start = 0, $s_End = 0)
    $s_Start = StringInStr($s_String, $s_Start) + StringLen($s_Start)
    Return StringMid($s_String, $s_Start, StringInStr($s_String, $s_End) - $s_Start)
EndFunc   ;==>_StringBetween1
oÝ÷ ÚÚºÚ"µÍ[ÈÔÔWÐ]ÙY[  ÌÍÜ×ÔÝ[Ë ÌÍÜ×ÔÝ    ÌÍÜ×Ñ[ ÌÍÚWÔ]^HH
NÈ ÌÍÚWÔ]^H]È[^HÙ[Ý[Y]HKÝÚÙHY][]ÈÝÝ[  ÌÍØWÐ^HHÝ[ÔYÑ^
    ÌÍÜ×ÔÝ[Ë ÌÎNÊÎÌÎNÈ    [È ÌÍÜ×ÔÝ    [È ÌÎNÊJÊJÎÌÎNÈ    [È ÌÍÜ×Ñ[ [È ÌÎNÊIÌÎNËÊBYÝÜ[Ý  ÌÍÚWÔ]^H[Ð^J   ÌÍØWÐ^JH[]  ÌÍØWÐ^VÌBYÐ^J ÌÍØWÐ^JH[]  ÌÍØWÐ^B[[

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

StringRegExp() or search the forum for "+Microsoft +XMLDOM" if the file has a real XML structure.

Cheers

Kurt

Man i've been trying to find StrinRegExp in the helpfile all day and i don't thinks it's there

care to offer a crash course?

Share this post


Link to post
Share on other sites

Hi,

go here http://www.autoitscript.com/autoit3/files/beta/autoit/

and then undocumentated.

There are several bugs in RegExp funcs, that is why they are out at the moment of beta.

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Thanks everyone for your replies. :)

You could do a stringSplit on the '<' sign then a "stringtrimleft" 7 characters

I'm assuming that returns the 7 characters after the <artist> tag, if so the problem with that is that some of the artists names are longer than 7 characters. Or am I missing something.

StringRegExp() or search the forum for "+Microsoft +XMLDOM" if the file has a real XML structure.

The text file is exactly as I posted, it kinda copies the xml structure. I have tried playing with this XML Wrapper but could not get it to work.

what about

Func _StringBetween1($s_String, $s_Start = 0, $s_End = 0)
    $s_Start = StringInStr($s_String, $s_Start) + StringLen($s_Start)
    Return StringMid($s_String, $s_Start, StringInStr($s_String, $s_End) - $s_Start)
EndFunc   ;==>_StringBetween1
oÝ÷ ÚÚºÚ"µÍ[ÈÔÔWÐ]ÙY[  ÌÍÜ×ÔÝ[Ë ÌÍÜ×ÔÝ    ÌÍÜ×Ñ[ ÌÍÚWÔ]^HH
NÈ ÌÍÚWÔ]^H]È[^HÙ[Ý[Y]HKÝÚÙHY][]ÈÝÝ[  ÌÍØWÐ^HHÝ[ÔYÑ^
    ÌÍÜ×ÔÝ[Ë ÌÎNÊÎÌÎNÈ    [È ÌÍÜ×ÔÝ    [È ÌÎNÊJÊJÎÌÎNÈ    [È ÌÍÜ×Ñ[ [È ÌÎNÊIÌÎNËÊBYÝÜ[Ý  ÌÍÚWÔ]^H[Ð^J   ÌÍØWÐ^JH[]  ÌÍØWÐ^VÌBYÐ^J ÌÍØWÐ^JH[]  ÌÍØWÐ^B[[
That looks interesting, but I can't get that to work either. :whistle: I'm kinda new to autoit.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Hi,

read the file into an Array with the func _FileReadToArray() Then build a loop and go through every line.

Use this func (example for one line)

$string ="<artist>AC DC</artist><album>Back in Black</album>"

MsgBox(0,"_StringBetween1", "Artist: " & _StringBetween1($string,'<artist>', '</artist>') & @CRLF & "Album: " & _StringBetween1($string,'<album>', '</album>'))

Func _StringBetween1($s_String, $s_Start = 0, $s_End = 0)
    $s_Start = StringInStr($s_String, $s_Start) + StringLen($s_Start)
    Return StringMid($s_String, $s_Start, StringInStr($s_String, $s_End) - $s_Start)
EndFunc   ;==>_StringBetween1

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Here run this with AutoIt Beta... And replace the txt file location or xml location with where ever it is.

$TxtLocation = @DesktopDir & '\test.txt'
$hFRead = FileRead($TxtLocation)
$Artist = _SRE_BetweenEX($hFRead, '<artist>', '<')
$Album = _SRE_BetweenEX($hFRead, '<album>', '<')

For $iCount = 0 To UBound($Artist) - 1
    MsgBox(64, 'Info:', 'Artist: ' & $Artist[$iCount] & @CR & 'Album: ' & $Album[$iCount])
Next

Func _SRE_BetweenEX($s_String, $s_Start, $s_End, $iCase = 'i')
    If $iCase <> 'i' Then $iCase = ''
    $a_Array = StringRegExp($s_String, '(?' & $iCase & _
                ':' & $s_Start & ')(.*?)(?' & $iCase & _
                ':' & $s_End & ')', 3)
    If @extended & IsArray($a_Array) Then Return $a_Array
    Return SetError(1, 0, 0)    
EndFunc

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Both work perfectly, thank you both so much.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

StringRegExp() or search the forum for "+Microsoft +XMLDOM" if the file has a real XML structure.

$ans = StringRegExp($b,"(?:(?: <artist>)(.+?)(?: </artist>))(?:(?:<album>)(.+?)(?:</album>))",3) Edited by jefhal

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

$ans = StringRegExp($b,"(?:)?: <artist>)(.+?)(?: </artist>))(?:)?:<album>)(.+?)(?:</album>))",3)

Looks funny with the smileys "expanded" :whistle:

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Looks funny with the smileys "expanded" :whistle:

Yeah, and I tried three different ways of avoiding that from happening: quote/autoit/code

Too lazy to create a hyperlink... :)


...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

Yeah, and I tried three different ways of avoiding that from happening: quote/autoit/code

Too lazy to create a hyperlink... :whistle:

Just uncheck the 'Enable Emoticons'

Share this post


Link to post
Share on other sites

Just uncheck the 'Enable Emoticons'

Thanks, Paulie! Done.

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

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
Sign in to follow this  
Followers 0