To preface, this is not my code and am just trying to resolve it. I am not accustomed to Python (just have been introduced with it recently with this project)
We run python scripts via UiPath. We upgraded the version of all python dependencies to Py3.10 which meant I had to convert the scripts using 2to3 as well, I've pinpointed the issue but I don't have any idea why this part of the script would fail.
Debugging technique used - slip ctypes.windll.user32.MessageBoxW in between lines of the script.
Here's the part of the code that was no longer able to show the message box (which I assumed is where this problematic). Line formatted as bold.
dObj = open(param_response_result_file)
ogdata = json.load(dObj)
ogdata['results']['fields'] = field_value_dictionary
with open(param_response_result_file, 'w+') as outfile:
**json.dump(ogdata, outfile)**
outfile.write("\n")
What can be the root causes for this line to fail? I've checked param_response_result_file and it contains the correct values, what more can I check to trace why this json.dump is failing?
Thank you
EDIT I found the exception being thrown:
Object of type bytes is not JSON serializable
How do I resolve this? I am aware that there has been variable changes between Py 2 to 3 upgrade with json instantiations, but how do I get it to pass the correct variable?
I am trying to pipe the output of xz to a custom python script:
xz -cd file.pcap.xz | myscripy.py
However, I get an error when the script attempts to run this line:
#!/usr/bin/env python2.7
from __future__ import print_function
import pcap
import io
STDIN_ALIAS = '/proc/self/fd/0'
pcap.pcap(io.open(STDIN_ALIAS, 'r'))
and received an error
pcap.pcap(io.open(STDIN_ALIAS, 'r'))
File "pcap.pyx", line 196, in pcap.pcap.__init__
TypeError: expected string or Unicode object, _io.TextIOWrapper found
I am on Ubuntu 18.04 and running under python 2.7.
You can't use Python to pass in packets from a file to pcap.pcap(). The pypcap library you are using is a thin wrapper around the pcap_open_offline() and pcap_create() C functions, and offers no facilities for passing in a Python file object. This wrapper only accepts a filename or a network interface name, nothing else.
The pcap_open_offline() function does accept - as an alias for stdin, so just pass that in directly:
import pcap
sniffer = pcap.pcap('-')
The error message already tell you what happened. You need a string to the pcap() function, not a file object. To fix this, try
pcap.pcap(io.open(STDIN_ALIAS, 'r').read())
But I am not sure this will work as your file might be binary instead of text. In such case, you may want to open with 'rb' instead of 'r' flag, and do some conversion afterwards (especially if you use Python 3 instead of Python 2.7).
I see another issue: your code is not portable as it depends on this:
STDIN_ALIAS = '/proc/self/fd/0'
A pythonic way to read the stdin is the follows (see Reading binary data from stdin)
import sys
string = sys.stdin.read()
Have you tried upgrading PyPcap to work on Python 3? This could help, since Unicode handling is a lot cleaner and less prone to surprises on Python 3. The appropriate package is available, at least on Debian (and probably derived distros as well). Look for: python3-pypcap.
I created two arrays with positive and negative emojis by using the emojis' unicode value:
positive = [
u'\U0001F600',
u'\U0001F601',
u'\U0001F602',
u'\U0001F923',
u'\U0001F603',
u'\U0001F604',
u'\U0001F605',
u'\U0001F606',
u'\U0001F609',
u'\U0001F60A',
u'\U0001F60B',
u'\U0001F60E',
u'\U0001F60D',
u'\U0001F618',
u'\U0001F617',
u'\U0001F619',
u'\U0001F61A',
u'\U0000263A',
u'\U0001F642',
u'\U0001F917',
u'\U0001F60F',
u'\U0001F60C',
u'\U0001F61B',
u'\U0001F61C',
u'\U0001F61D',
u'\U0001F924',
u'\U0001F643',
u'\U0001F62C']
negative = [
u'\U0001F610',
u'\U0001F611',
u'\U0001F636',
u'\U0001F644',
u'\U0001F60F',
u'\U0001F623',
u'\U0001F625',
u'\U0001F62E',
u'\U0001F910',
u'\U0001F62F',
u'\U0001F62A',
u'\U0001F62B',
u'\U0001F634',
u'\U0001F612',
u'\U0001F613',
u'\U0001F614',
u'\U0001F615',
u'\U0001F641',
u'\U0001F616',
u'\U0001F61E',
u'\U0001F61F',
u'\U0001F624',
u'\U0001F622',
u'\U0001F62D',
u'\U0001F626',
u'\U0001F627',
u'\U0001F628',
u'\U0001F629',
u'\U0001F630',
u'\U0001F631',
u'\U0001F635',
u'\U0001F621',
u'\U0001F620',
u'\U0001F637',
u'\U0001F912',
u'\U0001F915',
u'\U0001F922',
u'\U0001F927']
But when I print positive[0], for example, I get back this weird character instead of an emoji:
�
I'm working on an EC2 machine with Amazon Linux and using python-3.4.
Same code works as expected from my Macbook.
The issue is not a Python Issue. The Mac supports fonts in the terminal that can print those unicode characters.
Those same fonts are not supported in the case you are using.
If the device in question were to support those unicode values they would print properly.
I tested a standard macOS SSH Terminal to Ubuntu and that worked as does native Mac.
The issue was I was running under screen
Hello Daniel Haviv,
Try this if it works:
print(positive[0].encode('utf-8'))
You can read more here Python 2.x’s Unicode Support
Update: The previous solution did not worked for you try this:
import sys
import codecs
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
print(positive[0])
I found the solution from Setting the correct encoding when piping stdout in Python
I'm using python3 and geolite2, but I'm finding that I cannot pass in the IP address that I want to look up and I get the following error. I have tried converting to utf-8 and encoding, but am getting the same error.
from geoip import geolite2
ip_address = request.access_route[0] or request.remote_addr
print(">>>", ip_address)
ip_bytes = ip_address.encode('utf-8')
loc = geolite2.lookup(ip_bytes)
or
loc = geolite2.lookup(ip_address.encode())
Following error:
TypeError: 'str' does not support the buffer interface
What format should the IP go in as. In the original doc, it is string.
http://pythonhosted.org/python-geoip/
I would suggest trying the official Python geoip2 API or python-geoip-yplan. The latter is a fork of python-geoip with better Python 3 support.
First time using jira-python, and I get this error when I run just a simple initialization.
from jira.client import JIRA
jira = JIRA()
Error:
File "C:\Python32\lib\site-packages\jira\resources.py", line 146
if re.search(u"^User '(.*)' was not found in the system\.", error, re.U):
SyntaxError: invalid syntax
Any ideas? I am running this with py32
It looks very much like a bug.
They are claiming python 3 support, but there are u strings in the source code. Python 3 doesn't support u strings, all of the strings are unicode.
Consider submitting an issue to the python-jira bug tracker.
I guess you already installed Jira in your CMD, like:
$ pip install jira
Then just try something like the following:
from jira import JIRA
jira = JIRA('https://jira.atlassian.com') #Project URL
issue = jira.issue('KEY-1')
print issue.fields.project.key # 'KEY'
print issue.fields.summary # 'Issue Summary'