I just switched from python 2 to python 3. One of my python code written with python 2 failed. The error cannot be understand and I don't want to convert my python 2 code into python 3 compatible version. It would be better if I just rewrite the whole program.
However, the resource in google is confusing so I can't find any resource for python 3 telnetlib module. Does anyone know any resource for that?
I attached the error message when I executed the half-way converted python code:
File "E:\Python_Program\telnet.py", line 122, in <module>
tn.write(ftpUsername + "\n")
File "C:\Python32\lib\telnetlib.py", line 277, in write
if IAC in buffer:
TypeError: 'in <string>' requires string as left operand, not bytes
If your program is already working in python 2.x, then I think that what you really need is take a look at the Text Vs. Data Instead Of Unicode Vs. 8-bit section in the documentation since it explains why you're getting the traceback in the question:
The biggest difference with the 2.x situation is that any attempt to mix text and data in Python 3.0 raises TypeError
Related
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 am not well versed in python... my professor posted a piece of code that includes the following lines:
def formatOptions(options):
from string import joinfields, strip, split
options = joinfields(map(strip, split(strip(options), '\n')), ':')
return options
When I run this using idle 2.7, I do not get an error message. But when I run it using python 3, I get an error message. Is this a difference in the two versions, or do I have a problem with the python 3 build? This is the error message:
File "ml_exercise.py", line 46, in <module>
formatOptions(options))
File "ml_exercise.py", line 28, in formatOptions
from string import joinfields, strip, split
ImportError: cannot import name 'joinfields'
string functions where already mostly deprecated (in favor of the str class methods) in Python 1.6.0 (that is some 18 years ago). The idiomatic way to write this code is
options = ':'.join(part.strip() for part in options.strip().splitlines())
joinfields removed from Python in version 3. Just use the string join function like:
options = ':'.join(map(strip, split(strip(options), '\n')))
I'm newbie here and I wouldn't want to ask such a easy question as my first post but I don't know anything about Python even I'm a PHP/C programmer.
I have a python script in Figway tools which is called RegisterDevice.py to register my own sensor hardware to FIWARE Lab. But some code lines of that script doesn't work as I expected because of Python3.4. This may not be my problem but I don't have too much time to wait an official solution that's why I thought that I could resolve it as a person who is familiar to the programming.
I've searched on the web for solution but I couldn't find any exact solution for it yet. As far as I read bytes and unicode strings are two different types in Python3.x but I couldn't realize where I have to encode or maybe decode string to other type on the code. Maybe I have to do something else...
Here is the part of script which gave me error like above.
# Load the configuration file
with open(CONFIG_FILE,'r+') as f:
sample_config = f.read()
#config = ConfigParser.RawConfigParser(allow_no_value=True)
config = configparser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))
Error:
Traceback (most recent call last):
File "RegisterDevice.py", line 47, in <module>
config.readfp(io.BytesIO(sample_config))
TypeError: 'str' does not support the buffer interface
Firstly readfp() is deprecated in Python3 and you should use read_file().
The best way is probably using the read() function directly when you want to work with a file. You should set encoding as the second parameter if you expect non-ASCII characters inside the file.
The alternative is to read_string() and give it a string directly.
I have been doing work very similar to this, and I believe this script runs, but you will have to verify if it gives you the desired results:
import configparser
with open('.coveragerc','r+') as f:
#config = ConfigParser.RawConfigParser(allow_no_value=True)
config = configparser.RawConfigParser(allow_no_value=True)
config.readfp(f)
Previously I've used PyVisa1.4 in Python2.7, and everything works fine.
Now I need to use Pyvisa1.4 in Python3.2.
I knew that some syntax are changed in Python3.2. Therefore I use the 2to3 to convert the originalPysiva .py files into the new format which are supposed to fit the Python3.2.
But now, unexpected error is generated which is related to ctypes. And I read through the Pyvisa package .py files and try to fix this but still don't know how to do deal with this.
I'm just trying to use the simple get_instruments_list() command like below:
>>> import visa
>>> get_instruments_list()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
get_instruments_list()
File "C:\Python32\Lib\site-packages\pyvisa\visa.py", line 254, in get_instruments_list
vpp43.find_resources(resource_manager.session, "?*::INSTR")
File "C:\Python32\Lib\site-packages\pyvisa\vpp43.py", line 581, in find_resources
instrument_description)
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type
The MAIN problem I'm facing now is how to correctly use PyVisa in Python3.2.
PyVISA 1.5 (which is in beta now) provides, among other things, full Python 3 support. Take a look at the (new) documentation for instructions about how to download the latest development version at http://pyvisa.readthedocs.org/
The problem is the str that is passed as second argument.
In Python 3 the str was radically changed in order to support unicode.
To correct this problem all strings before being passed to the DLL library must be encoded in ASCII.
Conversely, the return strings are returned as bytes should be converted back into str.
I have corrected this on the visa.py, on the
CR = "\r" replaces CR = b"\r"
LF = "\n" replaces LF = b"\n"
ResourceTemplate init
self.vi = vpp43.open(resource_manager.session, resource_name.encode("ASCII"),
keyw.get("lock", VI_NO_LOCK))
instead of
self.vi = vpp43.open(resource_manager.session, resource_name,
keyw.get("lock", VI_NO_LOCK))
Instrument.write
vpp43.write(self.vi, message.encode("ASCII"))
instead of
vpp43.write(self.vi, message)
conversely on the read_raw the final return is replaced by
return str(buffer)
and on get_instruments_list()
vpp43.find_resources(resource_manager.session, "?*::INSTR".encode("ASCII"))
The newest version of Pyvisa doesn't support Python3.2
Even thouhg you convert the syntax of Pyvisa1.4 for Python2.X to Python3.X by using 2to3 tool, it still won't work