h33th3n
h33th3n

Reputation: 267

Python Regex to pull multiple pieces of data out of a data structure

I need a regex to pull tidbits out of the following data structure. This data is in a javascript variable. I'm using BeautifulSoup and Mechanize to make the request and parse the page but I don't see how I can get what I need without a regex. More details follow below.

raw data:

var d = [[909.0546875,842.3125,32429,'TownID: 32429','GREY','circle_grey.png',970,'goldpimp\'s city','','N/A'],[1434.8890625,1365.41484375,32143,'TownID: 32143','GREY','circle_grey.png',899,'1..','','N/A'],[1553.92265625,1117.43046875,32326,'TownID: 32326','GREY','circle_grey.png',522,'Avacyns Pantheon','','N/A'],[1305.17265625,1328.6421875,28927,'TownID: 28927','GREY','circle_grey.png',3554,'Furiocity','','N/A'],...(cont.)

For example on the first line I need to pull TownID: 32429, 970, and goldpimp\'s city

I need to do this for the whole data structure to get each townID and associated information. Sorry for the newbie question but regex really racks my brain.

Upvotes: 0

Views: 114

Answers (1)

SonicARG
SonicARG

Reputation: 517

d is a list, you can access lists by indexing. So, why the regex? You don't need it. For getting your result:

for city in d:
  print "%s %s %s" % (city[3], city[6], city[7])

The print statement prints the text in console. Each %s will be replaced (in order) with a string from the right group (first %s will be replaced with city[3], second with city[6] and third with city[7]).

EDIT

OK, if d comes from a Javascript source, you need to convert to Python data using json.loads, store the result of it in a variable and access with the eariler method (see info about the Python's json module here for 2.7 and here for 3.3).

Upvotes: 2

Related Questions