Compile a Python script to convert a sqlAlchemy object to a dict tutorial, pythonsqlalchemy
When using sqlAlchemy to write a web application, it often uses json for communication. The object closest to json is dict. Sometimes it is more convenient to operate dict than to operate ORM objects, after all, you don't have to worry about the database session status.
Assume that there is a post table in the data
1. DictionariesDict = {' name ': ' Zara ', ' age ': 7, ' class ': ' First '}Dictionary converted to string, return: Print type (str (dict)), str (DICT)Dictionaries can be converted to tuples, returning: (' age ', ' name ', ' class ')Print tuple (dict)#字典能够转为元组, return: (7, ' Zara ', ' first ')Print tuple (dict.values ())Dictionary to list. return: [' age ', ' name ', ' class ']Print List (dict)Dictionary to ListPrint Dict.values2, meta-grouptup= (1, 2, 3, 4, 5)Tuple to String, return: (1, 2, 3,
Demand is such, because now the camera pixel is very high, the photo is very big, upload to the online album too slow, so to first turn the size, before are directly in the online search image processing software, and then think about it, since the program, this demand can be done by themselves.Python picture processing cool crooked, a few lines of code to fix, here is the Pillow library.The code below.#coding =utf-8from PIL Import image# requires Pillow Library import glob, Osin_dir = ' tmp_pho
PS: Online directly to find, paste out, convenient to use at any time, thanks to the people who share.#!/usr/bin/python#encoding:utf-8import socketimport codecsimport mmapfrom struct import pack, Unpackdef decode_str (old ): "Specifically for pure GBK encoded string decompression returns UTF8 string ' ' Try:return Unicode (old, ' GBK '). Encode (' Utf-8 ') except: # when the string decoding fails, and the most byte value is ' \x96 ', remove it,
Reply:How to use PiL
123
fromPIL importImageimg =Image.open(‘image.png‘).convert(‘LA‘)img.save(‘greyscale.png‘)
Using Matplotlib and the formula
1
Y‘ =0.299R +0.587G + 0.114B
You can do this:
1234567891011
importnumpy as npimportmatplotlib.pyplot as pltimport matplotlib.image as mpimgdefrgb2gray(rgb):returnnp.dot(rgb[...,:3], [0.299, 0.587, 0.114])img = mpimg.im
In practical applications, sometimes we need to convert the Python program to EXE file for easy useFirst, the use of PIP directly installed Pyinstaller, almost one-click installation, very convenient.After installation, the CD is located to the installation path, PYINSYTANLLER-V can view the version of Pyinstaller, if you do not want to copy the files need to be converted to the installation path or enter a
given a string:It is a sequence of several values separated by commas:' 192.168.1.1,192.168.1.2,192.168.1.3 'How do I convert a string to a list?MSTR = ['192.168.1.1'192.168.1.2 ' 192.168.1.3']Use the Str.split method:' 192.168.1.1,192.168.1.2,192.168.1.3 '>>> mstr.split (",") ['192.168.1.1' '192.168.1.2'192.168.1.3']to turn a string into a tuple:>>> mlist = Mstr.split (",")>>> Tuple (mlist) (' 192.168.1.1'192.168.1.2'192.168.1.3 ')>>>Blog Handling
The example in this article describes how Python converts a picture to Black-and-white effect through PIL. Share to everyone for your reference. The specific analysis is as follows:
PiL powerful, convert method can easily transform the picture, the following code can convert the picture to black and white effect
From PIL import image
image_file = Image.open
Convert a python datetime string to a Unix Timestamp
Today, I encountered a problem when I used python to write a small program for capturing web pages. The time string on the page, for example, '(17:38:49)', needs to be converted
UNIX timestamp. The strtotime function was previously written in PHP and was not found in Pytho
The Python socket module contains some useful IP conversion functions, which are described below:
Socket.ntohl (x) //Ntohl similar to C language (x)
Converts a 32-bit positive integer from the network sequence to the host byte order.
Socket.ntohs (x) //Ntohs similar to C language (x)
Converts a 16-bit positive integer from the network sequence to the host byte order.
SOCKET.HTONL (x) //htonl similar to C language (x)
Converts a 32-bi
This example describes how Python uses Pythonmagick to convert JPG images into ICO images. Share to everyone for your reference. The specific analysis is as follows:
The Pythonmagick module is used here, for more information on Pythonmagick modules and ImageMagick, please refer to: http://www.imagemagick.org/.The following code can be used to convert JPG images
Python to convert Chinese numbers to Arabic numerals,
This article describes how to convert Chinese numbers to Arabic numerals using Python. We will share this with you for your reference. The details are as follows:
I. Requirements
Today I wrote three thousand two hundred lines of code. Today I wrote 3200 lines of cod
Python script: An Introduction to converting excel into an xml file of testlink
Testlink only supports importing data in xml format, but some examples are displayed in excel. Test_link.py: Convert the excel file to the xml file of testlink, so that the excel file can also be imported to testlink.
Requirements for excel files: four columns: 1. Test Case name; 2. Test Preset conditions; 3. test steps; 4. Test
This article describes how to convert an image into a pdf using the reportlab module in python, for more information about how to convert an image to a pdf file, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:
#! /Usr/bin/env pythonimport osimport sysfrom reportlab. lib. pagesizes impor
Python uses PythonMagick to convert JPG images to ico images,
This example describes how python converts JPG images to ico Images Using PythonMagick. Share it with you for your reference. The specific analysis is as follows:
The PythonMagick module is used here. For more information about the PythonMagick module and ImageMagick module, see http://www.imagemagic
This example describes how Python uses Reportlab to convert a picture into a PDF. Share to everyone for your reference. The implementation method is as follows:
#!/usr/bin/env pythonimport osimport sysfrom reportlab.lib.pagesizes import A4, landscapefrom Reportlab.pdfgen Import canvasf = Sys.argv[1]filename = ". Join (F.split ('/') [-1:]) [: -4]f_jpg = filename+ '. jpg ' Print f_jpgdef conpdf (f_jpg): f_
A small program that used python to process XML Conversion the other day to convert XML and XSL to HTML.
Libxml2 is used. Therefore, libxml2 must be installed first.
#-*-Coding: MBCS -*-#! /Usr/bin/Python
Import libxml2, libxslt
Class compoundxml:Def _ init _ (Self ):Self. _ result = noneSelf. _ XSL = noneSelf. _ xml = noneDef do (self, xml_file_name, pai_file_na
This example describes how Python uses Pythonmagick to convert a JPG image to an ICO image. Share to everyone for your reference. The specific analysis is as follows:
The Pythonmagick module is used here, please refer to the Pythonmagick module and ImageMagick For more information: http://www.imagemagick.org/.The following code can be used to translate a JPG image into an ICO icon format.
#-*-Coding:u
Brief introduction:Use the displacement method to convert IP to number and type number to IP, using language as python2.7#!/usr/bin/env python# coding:utf-8def ip2num (IP): IP = [INT (x) for x in Ip.split ('. ')] Return ip[0] For the displacement algorithm, refer to:http://www.cnblogs.com/yyangblog/archive/2011/01/14/1935656.htmlPython uses the displacement method to co
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.