SoFunction
Updated on 2024-11-16

Python Pitfall Summary

written summary of a meeting

This article is used to record the process of learning Python encountered some small problems, if encountered is a relatively large problem will open a separate page to analyze and learn

There are potholes everywhere.

1. File reading open

# We open the file using the open method
xml = open("")
# When reading a file using the open command, the following error often occurs
Traceback (most recent call last):
  File "", line 84, in <module>
    for line in xml:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x8d in position 38: illegal multibyte sequence
# The reason for this error is that the system's default open encoding is not consistent with the file, and you need to open it with formatting parameters.
# For example, if the file is a utf-8 format file, the following format parameters are required:
xml = open("", encoding="utf-8")

2. Regular expressions \S and \S

First a question is posed to get the list of mailboxes in a string using a regular expression. Example: A message from csev@ to cwen@ about meeting @2PM

# We can pass a simple regular expression, here disregarding other complex conditions #
import re
str = 'A message from csev@ to cwen@ about meeting @2PM'
lst1 = ('\S+@\S+', s)
print(lst1) # ['csev@', 'cwen@']

# However, we find the same result with the following regular expression
lst2 = ('\\S+@\\S+', s)
print(lst2)

This is rather strange, because in regular expressions in other languages, the\S cap (a poem)\\S representations are not the same.\S represents a non-null character, while\\S Indicates a match string\SSo we made the following attempt:

'\S' == '\\S' # True
len('\\S') # 2
len('\S') # 2

Isn't that amazing! So I tried again.

'\s' == '\\s' # True
len('\\s') # 2
len('\s') # 2

'\n' == '\\n' # False
len('\\n') # 2
len('\n') # 1

We found\s cap (a poem)\n The situation is not the same, and with some searching, I found the following article:

Python regex '\s' vs '\s'

References in the text

Don't confuse python-level string-escaping and regex-level string-escaping. Since s is not an escapable character at python-level, the interpreter understand a string like \s as the two characters \ and s. Replace s with n, and it understands it as the newline character.
Don't confuse string escaping in Python with string escaping at the regular expression level. Since s is not an escapable character in Python, the interpreter understands a string such as \s as two characters, \ and s. Replace s with n, which it understands as a newline character.

There is no reference to a more authoritative statement, but it does react to the fact that if it is\s will be treated as two characters if the\\s on account of\\ is an escapable character that is treated as a\ One character.\\s It was also taken as a\s Two characters. That's why this is happening.

'\s' == '\\s' # True

3. Regular expression matching method match

While learning about regular expression matching rules, I realized that Python regular matching works a little differently than others, such as in the previous entry, where the\S together with\\S The question, and then there's the following:

Python's regular matching is ab initio; for example, if we want to match a phone number in the string

In JS you can use the following regular matches

// Using JS, we could have written it in the following way
'My cell phone number is 15900000000 don't tell anyone else or I'll tell someone that your number is 13900000000'.match(/1[0-9]{10}/g)
// (2) ['15900000000', '13900000000']

But it doesn't work so well if you put the same rule into Python

import re
str = 'My cell phone number is 15900000000 don't tell anyone else or I'll tell someone that your number is 13900000000'

# Wrong way to write
mah = ('1[0-9]{10}', str)
print(mah)
# None

Because Python's matchingmatch The default is to match from the beginning, and 1 is not necessarily the first letter of the given string.

# Another method, findall, should be used instead
mah = ('1[0-9]{10}', str)
print(mah)
# ['15900000000', '13900000000']

As you can see from this, many of Python's libraries offer methods that are different from those of other languages, and as a convert to Python from another language, you should actually test the methods or use them if you are familiar with them, rather than thinking in a stereotypical way that Python is just like any other language.

4. Help file pydoc

The help view for a library or method in Python can be done in the following way:

  • [Optional] Type python at the command line to enter the Python compilation environment.
  • utilizationdir (library, object) to see what methods the library or object can provide in the way of the
dir('String') # What are the operations for viewing strings

import re
dir(re) # See what operations are available in the Regular Expression Library
  • utilizationHelp (libraries, objects) to view help information for a library or object in the same way as the
import re
help(re) # View the help documentation for the regular expression library
dir() # Check out the regular expression's `match` Helpful information

If we are trying to write the help documentation to a text file, we can do so in thecommand line Use the command:

# Put re library help info into html document
python -m pydoc -w re

# windows you can output to a text file using the following methods
python -m pydoc re > d:\

More information about pydoc can be found in the official documentation.pydoc

5. String encode base64 encoding

The base64 encoding of strings on some tutorials looks like this:

str = "this is string example....wow!!!";
print "Encoded String: " + ('base64','strict')

# of expected outputs
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=

But this code reports an error:

LookupError: 'base64' is not a text encoding; use () to handle arbitrary codecs

It is understood that this wrong way of writing actually comes from the Python way of writing, but in Python the way of writing has changed, and the correct way of encoding base64 for strings should be:

import base64
str = "this is string example....wow!!!"

# Returns a version of the original string encoded as a byte string object
strb = ()
base64b = base64.b64encode(strb)
base64 = ()
print(base64)

6. Python calls C# DLLs.

I searched a lot in Baidu about the way Python calls C# dynamic link libraries, and most of them are the following code:

import clr
# ('') # dll in current directory
('') # dll in current directory

from DotNetWithPython import * # Import all classes in the DLL

if __name__ == '__main__':
    mainapp = MainForm() # initialization MainForm class object

Unfortunately, none of them work, and I'm not sure what's wrong with them or why they don't work. Is it possible that these are Python usages? (I'm learning Python.)

The following reflections were made:

The python clr is PythonNet, so is it possible to find the code directly on PythonNet or github?

So a search turned up the following address:/Try the codes given in it one by one, starting with this one:

from System import String
from  import *

We found that we would report an error:

Traceback (most recent call last):
  File "d:/Temp/PythonProjects/Demos/", line 10, in <module>
    from System import String
ModuleNotFoundError: No module named 'System'

Let's try to modify the code to:

import clr
from System import String
from  import *

To be sure, our calls to .NET-related classes must beimport clr Let's keep trying when trying the following code:

import clr
from  import Point
p = Point(5, 5)

It's reporting an error again:

d:/Temp/PythonProjects/Demos/:11: DeprecationWarning: The module was found, but not in a referenced namespace.
Implicit loading is deprecated. Please use ('').
  from import Point

From the error message given, we can see that we need to reference the space:

import clr
('')
from  import Point

p = Point(5, 5)
print(p)
# {X=5,Y=5}

At this point, we are basically sure that Python has no problem calling C#, so what if we can call our own dll library? Let's try to follow the previous system class reference:

import clr
('DotNetWithPython')
from DotNetWithPython import MainForm

mainapp = MainForm()

The result is reported as an error:

Traceback (most recent call last):
  File "d:/Temp/PythonProjects/Demos/", line 12, in <module>
    from DotNetWithPython import MainForm
ModuleNotFoundError: No module named 'DotNetWithPython'

So I thought again:

clr can call the class objects provided by .NET itself normally, but not mine The difference between the dynamic link libraries written by myself and those provided by .NET itself is that they are not in the system environment, and my own dll is in the current directory or other directories.

So we usedir(clr) Determined if there was any method available

import clr
dir(clr)

# ['AddReference', 'FindAssembly', 'GetClrType', 'ListAssemblies', 'Microsoft', 'Python', 'System', '_AtExit', '__class__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_extras', 'clrModule', 'clrmethod', 'clrproperty', 'e__NativeCall', 'getPreload', 'setPreload']

We found the way.FindAssembly It felt like it, so we tested it by following the way the system class was referenced in the previous section and this sentence:

import clr
('')
('DotNetWithPython')
from DotNetWithPython import MainForm

mainapp = MainForm()

It was still the same error, I was about to cry, so I had to go to PythonNet Github issues to find the answer, and I found that there are a lot of people raising this issue, and the problem is locked in the .net core, .net 5, and the problem does not appear in the .Net Framework, so I created a new project based on the . Net Framework, I created a new project based on the .Net Framework to do a simple test, and found that it does not report errors.

Now the problem was clear, but it wasn't solved, so I had to read the list of difficult issues line by line, and to my credit, I found this threadissues 1536, gave a clear statement thatPythonnet 2.5 does not support .NET 5It is supported in v3 previews.

Okay, so I usedpip list See all versions of Python third-party libraries

C:\Users\Administrator>pip list
Package          Version
---------------- ----------
click            7.1.2
pip              22.0.3
pycparser        2.21
PyQt5            5.15.4
pyqt5-plugins    5.15.4.2.2
PyQt5-Qt5        5.15.2
PyQt5-sip        12.9.1
pyqt5-tools      5.15.4.3.2
python-dotenv    0.19.2
pythonnet        2.5.2
qt5-applications 5.15.2.2.2
qt5-tools        5.15.2.1.2
setuptools       41.2.0

Sure enough, the version of pythonnet is 2.5.2. I downgraded the project and found that .net core is only supported when the version is net core, not .NET 5.

So if you're using a version of pythonnet, don't try to use a higher version of .net core to implement your functionality, or you'll need to update pythonnet to a higher version!

Keep looking.issues 1536, found that even after updating the version there was still a problem, and tracked down theissues 1473 I tried to upgrade pythonnet to the previews version but got an error and it didn't work, so I didn't continue to test subsequent features.

summarize

To this point this article on Python pit stepping summary of the article is introduced to this, more related pit stepping content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!