Vincent map html output not valid html - python

I'm writing some code with Python and Vincent to display some map data.
The example from the docs looks like this:
import vincent
county_topo = r'us_counties.topo.json'
state_topo = r'us_states.topo.json'
geo_data = [{'name': 'counties',
'url': county_topo,
'feature': 'us_counties.geo'},
{'name': 'states',
'url': state_topo,
'feature': 'us_states.geo'}]
vis = vincent.Map(geo_data=geo_data, scale=3000, projection='albersUsa')
del vis.marks[1].properties.update
vis.marks[0].properties.update.fill.value = '#084081'
vis.marks[1].properties.enter.stroke.value = '#fff'
vis.marks[0].properties.enter.stroke.value = '#7bccc4'
vis.to_json('map.json', html_out=True, html_path='map_template.html')
Running this code outputs an html file, but it's formatted improperly. It's in some kind of python string representation, b'<html>....</html>'.
If I remove the quotes and the leading b, the html page works as expected when run through the built in python server.
What's wrong with my output statement?

From the Docs:
A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the
literal should become a bytes literal in Python 3 (e.g. when code is
automatically converted with 2to3). A 'u' or 'b' prefix may be
followed by an 'r' prefix.
You can slice it using:
with open('map_template.html', 'w') a f:
html = f.read()[2:-1]
f.truncate()
f.write(html)
This will open your html file,
b'<html><head><title>MyFile</title></head></html>'
And remove the first 2 and last character, giving you:
<html><head><title>MyFile</title></head></html>

Related

"Expecting ',' delimiter". Everything I've tried is not working

My Code:
with open('music_queue.json', 'r') as f:
data = f.read()
list_str = data.split('\n')
print(list_str)
db = []
for d in list_str:
db.append(json.loads(d))
My raw JSON:
{"guild_id" : 00000, "song_list" : []}
I have tried doing:
data = data.replace('\"', '\\\"')
only for me to have this error:
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
I've been at this for hours. What's going on?
Also, apologies if this has already been answered. I literally couldn't find anything here that I haven't tried already.
Text inside json files must follow JSON standard and 00000 (without quotes to be marked as string) is not a valid value - replace it with 0 or "00000".
When you open a valid json file you can load the contents straight into a dictionary, like this:
with open('music_queue.json', 'r') as f:
data = json.load(f)
Example of valid json file:
{"guild_id" : 10000, "song_list" : []}
P.S. You have to use double quotes "" inside json files instead of single quotes ''
Expecting property name enclosed in double quotes
Let's understand this error, it simply says the property name has to be in double quotes. Now this refers to your JSON file since 00000 is not valid. If it was a number 0 would have been enough. But having 4 zeros is making it read it as a string
Strings in json need to be enclosed in double quotes
Change the json to:
{"guild_id" : "00000", "song_list" : []}
Hope it helps!

Python JSON encoding invalid json format

i've stucked, i have a json with symbols like ' in values and syntax with ' and "
Example mix double qoute and single qoutelink
json ={
'key': "val_'_ue",
'secondkey': 'value'
}
With json loads and json dumps i got a str type not a dict to iterate, any ideas how i get it fixed?
print(postParams)# = {'csrf-token': "TOKEN_INCLUDES_'_'_symbols", 'param2': 'params2value'}
jsn_dict2 = json.loads(json.dumps(postParams))
print(type(jsn_dict2)) # ERROR HERE why str and not dict
for key, val in jsn_dict2.items():
print("key="+str(key))
you dont need to dumps() an already string json data:
jsn_dict = json.loads(json.dumps(res))
should be :
jsn_dict = json.loads(res)
UPDATE
according to comments the data is looks like so:
postParams = "{'csrf-token': \"TOKEN_INCLUDES_'_'_symbols\", 'add-to-your-blog-submit-button': 'add-to-your-blog-submit-button'}"
so i found an library that can help damaged json string like this one:
first run :
pip install demjson
then this code can help you:
from demjson import decode
data = decode(postParams)
data
>>> {'csrf-token': "TOKEN_INCLUDES_'_'_symbols",
'add-to-your-blog-submit-button': 'add-to-your-blog-submit-button'}
In your json u have missed the "," comma separation between two keys. The actual structure of the JSON is
json_new ={
'key': "val_'_ue",
'secondkey': 'value'
}
use
json_actual=json.dumps(json_new)
to read,
json_read=json.loads(json_actual)

How to remove a particular character from a txt file?

Have a txt with contents :
{
hello : 1,two:three,four:five,six:seven,
}
how to remove the last , in the above string ?
while using them as dictionaries for further. it cant be parsed because of the last delimiter.
code :
import json
d2=json.load(open(test.txt))
i cant change the source code. coz i am extracting data from a json file(json.dump) and creating a new json. is there any way of doing that other than dump/changing the source code
This removes the last , in your string without the need of any further import's.
with open('test.txt', 'r') as f:
s = f.read()
s = s[::-1].replace(',', '', 1)[::-1]
the output of s is then:
{
hello : 1,two:three,four:five,six:seven
}
Simple replace should work fine.
broken_json = '''{
hello : 1,two:three,four:five,six:seven,
bye : 42,ick:poo,zoo:bar,}'''
j = broken_json.replace(',}', '}').replace(',\n}','\n}')
The result at this point is still not valid JSON, because the dictionary keys need to be quoted; but this is outside the scope of your question so I will not try to tackle that part.
string ='{hello : 1,two:three,four:five,six:seven,}'
length = len(string)
for i in range(length):
if(string[i] == ','):
string2 = string[0:i] + string[i + 1:length]
print (string2)
The output type would be a string. So, convert it to a dict later.
import re
string ='{hello : 1,two:three,four:five,six:seven,}'
match = re.search(r'(.*),[^,]*$', string)
print (match.group(1)+"}")
Try this #selcuk for an efficient one

Python: remove double quotes from JSON dumps

I have a database which returns the rows as lists in following format:
data = ['(1000,"test value",0,0.00,0,0)', '(1001,"Another test value",0,0.00,0,0)']
After that, I use json_str = json.dumps(data) to get a JSON string. After applying json.dumps(), I get the following output:
json_str = ["(1000,\"test value\",0,0.00,0,0)", "(1001,\"Another test value\",0,0.00,0,0)"]
However, I need the JSON string in the following format:
json_str = [(1000,\"test value\",0,0.00,0,0), (1001,\"Another test value\",0,0.00,0,0)]
So basically, I want to remove the surrounding double quotes. I tried to accomplish this with json_str = json_str.strip('"') but this doesn't work. Then, I tried json_str = json_str.replace('"', '') but this also removes the escaped quotes.
Does anybody know a way to accomplish this or is there a function in Python similiar to json.dumps() which produces the same result, but without the surrounding double quotes?
You are dumping list of strings so json.dumps does exactly what you are asking for. Rather ugly solution for your problem could be something like below.
def split_and_convert(s):
bits = s[1:-1].split(',')
return (
int(bits[0]), bits[1], float(bits[2]),
float(bits[3]), float(bits[4]), float(bits[5])
)
data_to_dump = [split_and_convert(s) for s in data]
json.dumps(data_to_dump)

Python: json.loads returns items prefixing with 'u'

I'll be receiving a JSON encoded string from Objective-C, and I am decoding a dummy string (for now) like the code below. My output comes out with character 'u' prefixing each item:
[{u'i': u'imap.gmail.com', u'p': u'aaaa'}, {u'i': u'333imap.com', u'p': u'bbbb'}...
How is JSON adding this Unicode character? What's the best way to remove it?
mail_accounts = []
da = {}
try:
s = '[{"i":"imap.gmail.com","p":"aaaa"},{"i":"imap.aol.com","p":"bbbb"},{"i":"333imap.com","p":"ccccc"},{"i":"444ap.gmail.com","p":"ddddd"},{"i":"555imap.gmail.com","p":"eee"}]'
jdata = json.loads(s)
for d in jdata:
for key, value in d.iteritems():
if key not in da:
da[key] = value
else:
da = {}
da[key] = value
mail_accounts.append(da)
except Exception, err:
sys.stderr.write('Exception Error: %s' % str(err))
print mail_accounts
The u- prefix just means that you have a Unicode string. When you really use the string, it won't appear in your data. Don't be thrown by the printed output.
For example, try this:
print mail_accounts[0]["i"]
You won't see a u.
Everything is cool, man. The 'u' is a good thing, it indicates that the string is of type Unicode in python 2.x.
http://docs.python.org/2/howto/unicode.html#the-unicode-type
The d3 print below is the one you are looking for (which is the combination of dumps and loads) :)
Having:
import json
d = """{"Aa": 1, "BB": "blabla", "cc": "False"}"""
d1 = json.loads(d) # Produces a dictionary out of the given string
d2 = json.dumps(d) # Produces a string out of a given dict or string
d3 = json.dumps(json.loads(d)) # 'dumps' gets the dict from 'loads' this time
print "d1: " + str(d1)
print "d2: " + d2
print "d3: " + d3
Prints:
d1: {u'Aa': 1, u'cc': u'False', u'BB': u'blabla'}
d2: "{\"Aa\": 1, \"BB\": \"blabla\", \"cc\": \"False\"}"
d3: {"Aa": 1, "cc": "False", "BB": "blabla"}
Those 'u' characters being appended to an object signifies that the object is encoded in Unicode.
If you want to remove those 'u' characters from your object, you can do this:
import json, ast
jdata = ast.literal_eval(json.dumps(jdata)) # Removing uni-code chars
Let's checkout from python shell
>>> import json, ast
>>> jdata = [{u'i': u'imap.gmail.com', u'p': u'aaaa'}, {u'i': u'333imap.com', u'p': u'bbbb'}]
>>> jdata = ast.literal_eval(json.dumps(jdata))
>>> jdata
[{'i': 'imap.gmail.com', 'p': 'aaaa'}, {'i': '333imap.com', 'p': 'bbbb'}]
Unicode is an appropriate type here. The JSONDecoder documentation describe the conversion table and state that JSON string objects are decoded into Unicode objects.
From 18.2.2. Encoders and Decoders:
JSON Python
==================================
object dict
array list
string unicode
number (int) int, long
number (real) float
true True
false False
null None
"encoding determines the encoding used to interpret any str objects decoded by this instance (UTF-8 by default)."
The u prefix means that those strings are unicode rather than 8-bit strings. The best way to not show the u prefix is to switch to Python 3, where strings are unicode by default. If that's not an option, the str constructor will convert from unicode to 8-bit, so simply loop recursively over the result and convert unicode to str. However, it is probably best just to leave the strings as unicode.
I kept running into this problem when trying to capture JSON data in the log with the Python logging library, for debugging and troubleshooting purposes. Getting the u character is a real nuisance when you want to copy the text and paste it into your code somewhere.
As everyone will tell you, this is because it is a Unicode representation, and it could come from the fact that you’ve used json.loads() to load in the data from a string in the first place.
If you want the JSON representation in the log, without the u prefix, the trick is to use json.dumps() before logging it out. For example:
import json
import logging
# Prepare the data
json_data = json.loads('{"key": "value"}')
# Log normally and get the Unicode indicator
logging.warning('data: {}'.format(json_data))
>>> WARNING:root:data: {u'key': u'value'}
# Dump to a string before logging and get clean output!
logging.warning('data: {}'.format(json.dumps(json_data)))
>>> WARNING:root:data: {'key': 'value'}
Try this:
mail_accounts[0].encode("ascii")
Just replace the u' with a single quote...
print (str.replace(mail_accounts,"u'","'"))

Categories

Resources