Jump to content

Translate PYTHON's code to AUTOIT


Luigi
 Share

Recommended Posts

Greetings,
 

Someone can help-me to translate this Python's code to AutoIt?

 

Python (source: https://repl.it/repls/InstructiveDarkslategreyJackrabbit)

str = 'age=12,name=bob,hobbies="games,reading",phrase="I\'m cool!"'
key = ""
val = ""
dict = {}
parse_string = False
parse_key = True
# parse_val = False
for c in str:
    print(c)
    if c == '"' and not parse_string:
        parse_string = True
        continue
    elif c == '"' and parse_string:
        parse_string = False
        continue
    if parse_string:
        val += c
        continue
    if c == ',': # terminate entry
        dict[key] = val #add to dict
        key = ""
        val = ""
        parse_key = True
        continue
    elif c == '=' and parse_key:
        parse_key = False
    elif parse_key:
        key += c
    else:
        val+=c
dict[key] = val
print(dict.items())

Python's output:

[('phrase', "I'm cool!"), ('age', '12'), ('name', 'bob'), ('hobbies', 'games,reading')]

AutoIt

#include-once
#include <Array.au3>
#include <StringConstants.au3>


Global $opt
$opt = "estado = """" , cep = """", idade=32, nome = ""Luismar"", campo=""campo = campo"""
$opt = "age=12,name=bob,hobbies=""games,reading"",phrase=""I\'m cool!"""
ConsoleWrite($opt & @LF)

Local $arr = StringSplit($opt, "", $STR_CHRSPLIT)

Local $key = ""
Local $val = ""
Local $dict = ObjCreate("Scripting.Dictionary")
Local $parse_string = False
Local $parse_key = True
Local $c


For $ii = 1 To $arr[0]
    $c = $arr[$ii]
    If $c == '"' And Not $parse_string Then
        $parse_string = True
        ContinueLoop
    ElseIf $c == """" And $parse_string Then
        $parse_string = False
        ContinueLoop
    EndIf

    If $parse_string Then
        $val &= $c
        ContinueLoop
    EndIf

    If $c = "," Then
        $dict.Add($key, $val)
        $key = ""
        $val = ""
        $parse_key = True
        ContinueLoop
    ElseIf $c == "=" And $parse_key Then
        $parse_key = False
    ElseIf $parse_key Then
        $key &= $c
    Else
        $val &= $c
    EndIf
Next
$dict.Add($key, $val) ; missing this line...

For $each In $dict
    ConsoleWrite($each & " = " & $dict.Item($each) & @LF)
Next

AutoIt's output

age=12,name=bob,hobbies="games,reading",phrase="I\'m cool!"
age = 12
name = bob
hobbies = games,reading

 

Best regards.

 

Edited by Luigi

Visit my repository

Link to comment
Share on other sites

One of the large number of ways to achieve that parsing:

Local $opt1 = 'estado = "" , cep = "", idade=32, nome = "Luismar", campo="campo = campo"'
Local $opt2 = 'age=12,name=bob,hobbies="games,reading",phrase="I''m cool!"'

Local $s
$s = StringRegExp($opt1, '(?m)([^=]+?) ?= ?(?|"([^"]*?)"|([^,]*?)) ?(?:,|$)', 3)
_ArrayDisplay($s)
$s = StringRegExp($opt2, '(?m)([^=]+?) ?= ?(?|"([^"]*?)"|([^,]*?)) ?(?:,|$)', 3)
_ArrayDisplay($s)

If you actually need a {key, value} scripting dictionary from the result, build one from the output array.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

You may have to slightly adapt the pattern if there are different spacings between elements parts.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
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
 Share

×
×
  • Create New...