Format functionThis is a method of string formatting, such as Str.format ().The basic syntax is to replace the previous% with {} and: . Two main uses are shown below: (1) such as: statement print ( "{:. 2f}" . Format3.1415926 (2) such as: statement "{1} {0} {1} ". Format ( "hello "" world " ' world Hello world ALSO: statement print ' {} * '. Format
Everyone good noon, because the new year has not returned to the state, for a long time did not share a wave of small knowledge, today, continue to share a wave of Python parsing log small script.First of all, the same first look at the log is a kind of ...are all in XML format, is not looking at the dizzy?? All right, let's analyze a wave first.1. Each paragraph begins with catalina-exec, then we divide it
Because often write some scheduled tasks and reports, often need to get yesterday's date, the number of days last month, or today's weeks, if there is a Python module is certainly convenient, today introduced two "datetime", "Calendar"
Introduction to the DateTime module
How to get the current year, month, day, week. Format output TimeOfficial Document PortalIn[25]:importdatetimein[26]:now=dat
with Python:1. First traverse all the files in the current folder that begin with ' part ';2. For each file, read each line, according to "," to split;3. Then read each part of the quotation mark in the middle of the section, the last time to take the part of the hour, where it is necessary to determine the number of hours is 1 or 2;4. Write a line on each line readHere is the specific to buy#coding: Utf-8import osfor root,dir,files in Os.walk ("./")
This article describes the Python format of the compressed JS file method. Share to everyone for your reference. The specific analysis is as follows:
The script can be compressed JS file format for some restore, of course, will not be perfect, temporarily do not deal with grammar problems, just for the convenience of Reading JS code
Lines = open ("Unformate
In Pycharm, when editing a program, often the code is correct, but do not know where the error, here in the following example to illustrate the importance of code format in the Pycharm.
Title: Write a function, accept a positive even number as a parameter, output 2 prime numbers, and the sum of these 2 primes equals the original positive even, there are many groups of eligible primes, then all output.
Import Math
def isprime (n):
m=int (MATH.SQRT
The function for formatting the date and time in python is datetime. datetime. strftime (); Convert from string to date type function: datetime. datetime. strptime (), two functions are involved in the format string of the date and time, as shown below:
% A abbreviated weekday name% A full weekday name% B abbreviated month name% B full month name% C Date and Time Representation appropriate for locale% D da
1, import wave used to read and write WAV files
It provides a convenient WAV format interface.
However, compression/decompression is not supported and mono/stereo is supported.
Read format:
Open (file[, mode])
If file is a string, open it or treat it as a class file object.
Mode can be default, and if the input parameter is a class file object, then File.mode will be the value of mode.
Mode optional paramet
Python2.6 begins with the addition of a function Str.format () that formats the string, which enhances the functionality of string formatting. The basic syntax is to replace the previous% with {} and: . The Format function can accept no more than one parameter, and the position can be out of order.You can also format numbers:Detailed Description: http://www.runoob.com/
Background:I have a lot of log data, every log has a date string, and I need to convert it to a DateTime format.The problem is that there are a variety of string formats in these logs, with 2017-05-25t05:27:30.313292255z, 2016-07-01t00:00:00 and other formats I haven't seen yet.At first I wrote a long list of if else to determine the format, but there was always something I missed.Finally on the Internet a search, found dateutil.parser.parse. You can
Gets the list of specified suffix files in the Python environment.SOURCE StackOverflowHere is a simple example of *.txt.
Using Glob (recommended)1 Import glob, Os 2 os.chdir ("/mydir")3 for in Glob.glob ( " *.txt " ):4 print(file)
Simple and practical os.listdir1 Import OS 2 for in Os.listdir ("/mydir"):3 if File.endswith (". txt"):4 print(file)View Code
If you need to traverse the directory, use Os.walk 1 import OS 2 fo
This article mainly introduces how to convert xml or xsl to html format using Python. This article provides the implementation code directly. if you need it, refer to a small program that uses python to process xml conversion the next day, used to convert xml and xsl into html.
Libxml2 is used. Therefore, libxml2 must be installed first.
# -*- coding: mbcs -*-
Image Restoration code in jpg format implemented by python,
Recently, the damaged jpg file is fixed for the customer, and the effect is acceptable, but it is not guaranteed to be applicable in any situation.
If you have damaged the photo, try it. If you can use it, leave a message for me.Copy codeThe Code is as follows:#-*-Coding: utf8 -*-#! /Usr/bin/env python
_
The interception section reads as follows:10.235186 11.321997 10.122339 11.810993 9.190236 8.904943 9.306371 9.847394 8.330131 8.340352How do I turn data into a matrix?The first step is to open the file by using the open () function:1 >>> filename='; 2 >>> fr=open (fileName);View Type:1>>> fr=open (fileName);2>>>Type (FR)3class '_io. Textiowrapper'>4>>>FR5'D://softwaretool/python/python_exercisecode/chap13_pca//testset.txt'Mode='R'en
= Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, = maroon, + = Dark Green, 18 = Dark Blue, Dark Yellow, almost brown), = Dark Magenta, = Teal, = Light gray, Dark Gray, the list goes On ... style = XLWT. Xfstyle () # Create the pattern Style.pattern = pattern# Add pattern to style Worksheet.write (0, 0, ' Cell Conte NTS ', style) Workbook.save (' Excel_workbook.xls ') Todo:things left to Document- Panes -- separate views which are always in view- Border Colors (documented above, but n
The files in the working directory need to be transcoding, and the starting encoding is GBK and needs to be converted to utf-8. A lot of files, manual conversion certainly not, use Python to write a script to achieve.
Your own way of writing, for Linux,
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 The 25 26
#!/usr/bin/python #coding =utf-8 Import sys import OS, os.path
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.