Translating .unpack from Python 2.7 to 3.9 - python

I am working on rewriting a piece of code that was written for python 2.7. I am having trouble with the line below. It doesn't give any errors, but the self.gear value doesn't look right.
self.gear = struct.unpack("I", self.mm[64:68])[0]

struct.unpack("I",some_bytes[0:4]) works exactly the same in py2 and py3

Related

Python code failing to execute Json.Dump after upgrading from 2.7 to 3.10

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?

Eror exucting script with python 3.5 but working fine with 3.8

I'm running the following script in Python 3.5 :)
def series_to_fac_details_xml(s):
return fac_details_xml_template.format(**s)
for index, row in df3.iterrows():
details = series_to_fac_details_xml(row)
with open(fr"C:\Users\Max12\Desktop\xml\pdfminer\UiPath\attachments\75090058\Status\Afgeleverd\{row['Output']}.xml", "w") as f:
f.write(fac_doc_template.format(fac_details=details))
the error it gives you is "invalid syntax"..
However, with python 3.8 it works just fine.
I have to run Python 3.5 because UiPath only supports python versions >3.6
Could you please help?
The problem is in this line:
with open(fr"C:\Users\Max12\Desktop\xml\pdfminer\UiPath\attachments\75090058\Status\Afgeleverd\{row['Output']}.xml", "w") as f:
You can't use f-strings in python 3.5 because they were added in python 3.6. Use another string formatting method instead like format().

Import glob, invalid syntax python

Okay, I'm a noob when it comes to Python, I have to learn this for work.. And so far, I'm looking at some small programs to list directories.
I'm using Python 3.2.1.. In the Python Shell, I make a new window and I put:
import glob
print glob.glob("/*.txt")
But when I "run module", I save it, and it always tells me Invalid syntax, and it highlights the 2nd glob in the code.. Why?? Any idea on how to fix this? I don't really understand why I have an error..
print is a function in Python 3. You can't use it as a statement like you would in the 2.x versions. Your code should work if written as:
import glob
print(glob.glob("/*.txt")) #Note the parens for print()

Python invalid syntax in print

Sorry I am not familar with Python...
It gives me the following error message
File "gen_compile_files_list.py", line 36
print 'java files:', n_src
^
SyntaxError: invalid syntax
I.e. caret points to last quote. What's wrong with it?
OS Windows 7, Python version 3.2.2
On Python 3, print is a function. You need this:
print('java files:', n_src)
print changed syntax between Python2 and Python3; it is now a function.
You would need to change:
print 'java files:', n_src
to
print('java files:', n_src)
Alternatively, you can try converting the code from Python2 to Python3 syntax with the 2to3 tool. Here is more information on the transition if you are interested. This way you can maintain a single code base that works on both versions.
As you are not familiar with python, try installing Python 2 instead and running the code with that.
print is a function in Python 3+. So:
print ('java files:', n_src)

Need tutorial for telnet module in python 3

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

Categories

Resources