Jump to content
Chris Mathews

Parsing a python collection into a delphi collection

Recommended Posts

I've had success running python scripts and returning class properties to Delphi.

 

I'm struggling with where to start to return a python tuple (containing an OrderedDict and a string to a Delphi collections.  The module is usaddress and I'm using as follows:

script 

from collections import OrderedDict
import usaddress

od
 = OrderedDict()
od = usaddress.tag('5757  Woodway Ave APT 150 WEST PALM BEACH FL 37205 ')
 
print(od)
print(type(od))
first = od[0]
print(first)
print(type(first))
second = od[1]
print(second)
print(type(second))

 

and the output is this:

(OrderedDict({'AddressNumber': '5757', 'StreetName': 'Woodway', 'StreetNamePostType': 'Ave', 'OccupancyType': 'APT', 'OccupancyIdentifier': '150', 'PlaceName': 'WEST PALM BEACH', 'StateName': 'FL', 'ZipCode': '37205'}), 'Street Address')
<class 'tuple'>
OrderedDict({'AddressNumber': '5757', 'StreetName': 'Woodway', 'StreetNamePostType': 'Ave', 'OccupancyType': 'APT', 'OccupancyIdentifier': '150', 'PlaceName': 'WEST PALM BEACH', 'StateName': 'FL', 'ZipCode': '37205'})
<class 'collections.OrderedDict'>
Street Address
<class 'str'>

 

 

Guiding me to a starting point would be appreciated.

Share this post


Link to post
Posted (edited)

I am not exactly sure what you are trying to do,  Also the 4th line in your script is doing nothing since you are assigning a new value in the next statement.

 

However, if you want to access these values in Delphi and store them say in a Delphi data structure, the easiest way is using VarPyth.  For instance to access the AddressNumber, after running the above script, you use something like:

 

var AddressNumber := MainModule.od[0]['AddressNumber'];

Edited by pyscripter

Share this post


Link to post

Followup Question:

 

Why am I getting an Incompatible type error integer and string on var AddressNumber := MainModule.od[0]['AddressNumber'];

Share this post


Link to post
Posted (edited)

As you can see from your output AddressNumber is a string (as well as the other fields).  You need to convert it to an integer.

Edited by pyscripter

Share this post


Link to post

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

×