In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to explain the problem of Python object conversion, I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Most of the formats of Python objects are the same as those of C, requiring that the types of values to be output meet the needs of the format. You can write part of your programs in C or C++, and then use them in your Python programs, which can not only avoid errors but also greatly improve work efficiency.
Converts a string such as' 123'to the corresponding integer value 123. However, reading and writing is much more complicated when you want to save more complex data types such as lists, dictionaries, or class instances. Python is designed so that programmers do not have to repeatedly write code to debug and save complex data types, and it provides a standard module called pickle.
This amazing module can convert almost any Python object into a string representation, a process called pickling, and restoring an object from its string representation is called recovery. Between pickling and anti-pickling, the string representation of an object can be saved in a file or data, or even transferred to a remote computer through a network connection.
Pickle is a standard way to save Python objects and be called later by other programs or the same program when it is run. The term for this practice is "persistent objects". Because pickle is widely used, many authors of Python extension modules are careful to make sure that newly added data types such as matrices can be properly pickled and restored.
The * argument is a string containing the file name, and the second argument is the string in which the file is opened. The pattern'r 'means to read,' w' means to write only (existing files with the same name are cleared), and'a 'means to open files and add them at the end.
'ringing 'means that you can open a file that can be read or written. The open mode parameter is optional and defaults to'r 'mode. Adding'b'to the mode in Windows and Macintosh means to open the file in binary format, such as' rb'','wb', 'rroomb'. Windows treats text files differently from binary files.
The newline characters in a text file change when reading and writing. This behind-the-scenes modification of file data does not affect ASCII text files, but destroys the data of binary data such as JPEG or ".exe" files. Be sure to use binary format to read and write files like this. The exact description of the text pattern in Macintosh depends on the C library used.
Here are two ways to write squared and cubic tables:
> import string > for x in range (1,11):. Print string.rjust (`x`, 2), string.rjust (`xx`, 3),. # the comma at the end of the previous line indicates no line wrapping. Print string.rjust (`xroomxx`, 4)... 11 12 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 5 12 9 81 729 10 1000 > > for x in range (1Magne11) Print'%2d% 3D% 4d'% (x, xonomx, x*x*x)... 1 1 12 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 1000aspx
Notice that there is an extra space between the items in the print output, which is the rule of print. This example shows the use of the function string.rjust (), which aligns a string to the right with a specified width and fills it with spaces on the left. Similar functions include string.ljust () and string.center (). These functions do not output, but simply return the converted string. If the input string is too long, it will not be truncated but will be returned as is.
This processing may invalidate your column alignment, but it may be better than truncation, and the result of truncation is that we see an incorrect value. (if you really need to truncate, you can always add another layer of snippet, such as string.ljust (xmenn) [0VRN]).
After reading the above, have you mastered how to explain the problem of Python object conversion? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.