I got a HeaderParseError as below.What's wrong with it?
>>> from email import Header
>>> s= "=?UTF-8?B?6KGM6KGM5ZyI5Li65oKo5o6o6I2Q5Lul5LiL6IGM5L2N77yM?==?UTF-8?B?56Wd5oKo5om+5Yiw5aW95bel5L2c77yB44CQ6KGM6KGM5ZyI44CR?="
>>> src = Header.decode_header(s)
This is the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/email/header.py", line 108, in decode_header
raise HeaderParseError
email.errors.HeaderParseError
You are trying to parse two headers at once:
"=?UTF-8?B?6KGM6KGM5ZyI5Li65oKo5o6o6I2Q5Lul5LiL6IGM5L2N77yM?="
and
"=?UTF-8?B?56Wd5oKo5om+5Yiw5aW95bel5L2c77yB44CQ6KGM6KGM5ZyI44CR?="
removing one of them will do the job. If you want to parse all of them - you have to split them first
Related
I am trying to use the Element Tree modules but I end up to some Error which I can't understand.
My code here is based on the Python documentation itself, Python Element Tree doc ,somehow it gave me an error when trying to run the script;
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
file_name_xml = "curl-result.xml"
tree = ET.parse(file_name_xml)
tree.getroot()
When I run this code:
./python2.6 modify_xml_file.py
Then, it gave me this error;
Traceback (most recent call last):
File "modify_xml_file.py", line 8, in <module>
tree = ET.parse(file_name_xml)
File "<string>", line 45, in parse
File "<string>", line 32, in parse
SyntaxError: not well-formed (invalid token): line 1, column 4
The version of cElementTree included in Python 2.6 throws a SyntaxError exception for malformed XML:
>>> with open('bad.xml', 'w') as badxml:
... badxml = '<foobar\n'
...
>>> import xml.etree.cElementTree as ET
>>> tree = ET.parse('bad.xml')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 45, in parse
File "<string>", line 32, in parse
SyntaxError: no element found: line 1, column 0
This is a bug in the C acceleration code fixed in Python 2.7. The (slower) Python parser throws a more helpful error:
>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse('bad.xml')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.6/xml/etree/ElementTree.py", line 862, in parse
tree.parse(source, parser)
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.6/xml/etree/ElementTree.py", line 587, in parse
self._root = parser.close()
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.6/xml/etree/ElementTree.py", line 1254, in close
self._parser.Parse("", 1) # end of data
xml.parsers.expat.ExpatError: no element found: line 1, column 0
Fix your XML input file.
What changed in 2.7 is that ElementTree was updated to version 1.3, a version that improved the parser, introducing a new ParseError exception, which is a subclass of SyntaxError.
I am successful in extracting the response from a JSON. However, I am unable to list all or extract what I need on the key and its pair
Below is my code:
import requests
response = requests.get("https://www.woolworths.com.au/apis/ui/Product/Specials/half-price?GroupID=948&isMobile=false&pageNumber=1&pageSize=36&richRelevanceId=SP_948&sortType=Personalised")
data = response.json()
I tried to do data['Stockcode']
but no luck or I use data['Product']
It says:
>>> data['Product']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Product'
>>> data['Products']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Products'
try:
>>> data['Items'][0]['Products']
Print data and see its data structure How its constructed then you can extract values as per your need
I could successfully connect to reddit's servers with oauth2 some time ago, but when running my script just now, I get a KeyError followed by a NoSectionError. Code is below followed by exceptions, (The code has been reduced to its essentials).
import praw
# Configuration
APP_UA = 'useragent'
...
...
...
r = praw.Reddit(APP_UA)
Error message:
Traceback (most recent call last):
File "D:\Directory\Python\lib\configparser.py", line 843, in items
d.update(self._sections[section])
KeyError: 'useragent'
A NoSectionError occurred when handling the above exception.
"During handling of the above exception, another exception occurred:"
'Traceback (most recent call last):
File "D:\Directory\Python\Projects\myprj for Reddit, globaloffensive\oddshotcrawler.py", line 19, in <module>
r = praw.Reddit(APP_UA)
File "D:\Directory\Python\lib\site-packages\praw\reddit.py", line 84, in __init__
**config_settings)
File "D:\Directory\Python\lib\site-packages\praw\config.py", line 47, in __init__
raw = dict(Config.CONFIG.items(site_name), **settings)
File "D:\Directory\Python\lib\configparser.py", line 846, in items
raise NoSectionError(section)
configparser.NoSectionError: No section: 'useragent'
[Finished in 0.2s]
Try giving it a user_agent kwarg.
r = praw.Reddit(useragent=APP_UA)
I have code like this :
import requests
import xmltodict
Locations = ['http://129.94.5.93:49154/setup.xml', 'http://129.94.5.92:49154/setup.xml', 'http://129.94.5.93:49154/setup.xml', 'http://129.94.5.92:49154/setup.xml', 'http://129.94.5.95:80/description.xml']
for item in Locations:
r = requests.get(item)
reply = xmltodict.parse(r.text)
this gives me the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ssdpxml.py", line 57, in <module>
r = requests.get(item)
AttributeError: 'str' object has no attribute 'get'
But it works when I do this:
r=requests.get('http://129.94.5.95:80/description.xml')
Why do I get the above mentioned error??
I have checked that the file in my disk is the same as https://github.com/scipy/scipy/blob/master/scipy/misc/face.dat
but when I run this statement ,I have got this error. why ?
scipy.misc.face()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\scipy\misc\common.py", line 567, in face
data = bz2.decompress(rawdata)
ValueError: couldn't find end of stream
It's a bug (on Windows), see here
https://github.com/scipy/scipy/commit/7f0210fd28a2b3be79806d5cae462be52c1601e9