[Tutor] 2016-02-03 Filter STRINGS from urllib and Pass as VARAIBLE within PYTHON script
Alan Gauld
alan.gauld at btinternet.com
Wed Feb 3 04:39:55 EST 2016
On 03/02/16 01:30, knnleow GOOGLE wrote:
> advice on how to filter VARIABLES from output below.
>
> 01. need to strip out
> BEGIN_STRINGS b'{ and
> END_STRINGS }\n'
Are you sure those are actually part of the string and not just the
representation output by Python?
The b at the start usually means its a byte string and is not actually
present in the data. And the trailing \n is a newline character which
can be removed with the string rstrip() method.
Also the format of your data looks a lot like JSON?
If that's the case you may be better off using the json module
to parse it into Python objects. If you strip off the markers
you may be forcing yourself to write a complex string parser
when the json one could do the job for you.
>
> 02. need to filter out these FIELDS
> FIELD 1 "ip":"222.187.222.220"
> FIELD 2 "country_code":"CN"
> FIELD 3 "country_name":"China"
> FIELD 6 "city":"Shanghai"
> FIELD 8 "time_zone":"Asia/Shanghai"
>
If it is JSON data you will get a dictionary from which you
can read out (or ignore) any of the fields as you wish.
> 03. need to feed this into python script so as to generate a HTML File
> by date script was run. example "/fail2ban/2016-01-31.html"
I'm not sure what exactly you mean here. Can you elaborate?
What is the "this" that you want to "feed in" and what kind
of HTML file are you thinking of? Just a simple textual
list of entries? A table?? If you can store the data as
Python objects it is fairly easy to just print the strings
you want as needed.
> OUTPUT:
>
> b'{"ip":"222.187.222.220","country_code":"CN",
> "country_name":"China","region_code":"31",
> "region_name":"Shanghai_Shi","city":"Shanghai",
> "zip_code":"","time_zone":"Asia/Shanghai",
> "latitude":31.0456,"longitude":121.3997,"metro_code":0}\n'
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list