python commonly used functions summary

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_39232265/article/details/79183072

A string processing

Split string python

Python, the split () and The os.path.split () two functions, specifically effects include:

  • split (): Split string. Slicing string specified by delimiters and returns the string list (list) divided
  • os.path.split (): follow the path to the file name and path separated

1, split () function

>>>str = 'aas,sgsgwe,sagwe,dfgxzsf,wagwr'
#使用默认分隔符
>>>print(str.split())
['aas,sgsgwe,sagwe,dfgxzsf,wagwr']

#使用‘,’分隔符
>>>print(str.split(','))
['aas', 'sgsgwe', 'sagwe', 'dfgxzsf', 'wagwr']

#分割0次
>>>print(str.split(',',0))
['aas,sgsgwe,sagwe,dfgxzsf,wagwr']

#分割1次
>>>print(str.split(',',1))
['aas', 'sgsgwe,sagwe,dfgxzsf,wagwr']

#分割两次,并取序列为1的项
>>>print(str.split(',',2)[1])
sgsgwe

#分割最多次,可以不加参数
>>>print(str.split(',',-1))
['aas', 'sgsgwe', 'sagwe', 'dfgxzsf', 'wagwr']

#分割两次,并把三部分保存到三个变量里面
>>>str1,str2,str3 = str.split(',',2)
>>>print(str1)
aas
>>>print(str2)
sgsgwe
>>>print(str3)
sagwe,dfgxzsf,wagwr

2, os.path.split () function
to "/" is a delimiter final segmentation and extraction path "/" path or file content behind

>>>import os
>>>print(os.path.split('E:\\data\\db\\journal\\'))
('E:\\data\\db\\journal', '')
>>>print(os.path.split('E:\\data\\db\\journal'))
('E:\\data\\db', 'journal')

3, an example

>>> str="hello boy<[www.doiido.com]>byebye"  

>>> print str.split("[")[1].split("]")[0]  
www.doiido.com  

>>> print str.split("[")[1].split("]")[0].split(".")  
['www', 'doiido', 'com']  

python string supplement

>>>str = '''kkk
...hello
...world'''
>>>c
'kkk\nhello\nworld'
>>>print(c)
kkk
hello
world

Second, file read and write

Read and write files modes:

1, r is opened read-only file, the file must exist.
2, r + opened read-write file must exist.
3, w opened write-only file, if the file exists, the file length is cleared to 0, that is, the contents of the file will disappear. If the file does not exist, create the file.
4, w + Open to read and write files, if the file exists, the file length is cleared to 0, i.e., the file content will disappear. If the file does not exist, create the file.
5, a way open to additional write-only file. If the file does not exist, the establishment of the file, if the file exists, data is written to the file will be added to the end, that is, the original file contents are retained.
6, a + open writable way to attach a file. If the file does not exist, the establishment of the file, if the file exists, the data is written will be added to the end of the file, that file the original content will be retained.
7, the above-described aspect may be combined with a character string, character b, such as rb, w + b ab + or the like in combination, was added to tell the library b character file as a binary file to open, rather than plain text file.
8, t is the so-called windows platform-specific text mode (text mode), except that line breaks will automatically identify windows platform.
Unix-like platforms newline is \ n, and use the platform windows \ r \ n two ASCII characters for newline, uses internal python \ n to represent a newline character.
at rt mode, python when reading the text automatically \ r \ n is converted into \ n-.
the wt mode, Python will use \ r \ n for newline write file.

Reading file

To open a file with mode read a file object, using Python's built-in open () function, passing the file name and identifier:

f = open('/Users/michael/test.txt', 'r')

If the file does not exist, open () function will throw an IOError error and gives detailed information on the error code and tell you the file does not exist.
If the file is opened successfully, then call read () method can read the entire contents of a file, Python read the contents of memory, expressed as a str objects:

print(f.read())

result:

'Hello, world!'

The final step is to call close () method to close the file. After completion of the files must be closed, because the file object will occupy the resources of the operating system and the operating system at the same time the number of files that can be opened is limited:

 f.close()

Since when are likely to produce IOError file read and write, once the error, back f.close () will not be called. Therefore, in order to ensure whether or not the error could properly close the file, we can use try ... finally be realized:

try:
    f = open('/path/to/file', 'r')
    print(f.read())
finally:
    if f:
        f.close()

But every so realistic in too cumbersome, so, Python introduced with statement to automatically help us call the close () method:

with open('/path/to/file', 'r') as f:
    print(f.read())

Call read () will read the entire contents of a one-time file, if the file has 10G, memory to burst, so to be on the safe side, you can call repeatedly read (size) method, each read up to size bytes of content . In addition, the call to readline () can read each line of text calling readlines () time to read all the contents and press OK to return list. Therefore, according to need to decide how you want to call.
It can also be used the readline binary, binary files to get the normal display, to add decode ( 'utf-8')
, for example:. Str = readline () decode ( 'utf-8')

If the file is small, read () reads a one-time most convenient; if you can not determine the file size, repeatedly calling read (size) safer; if it is the configuration file, call readlines () the most convenient:

for line in f.readlines():
    print(line.strip()) # 把末尾的'\n'删掉

Write file

Write files and read documents is the same, the only difference is that the call to open () function, the incoming identifier 'w' or 'wb' for write text files or write binary files:

f = open('/Users/michael/test.txt', 'w')
f.write('Hello, world!')
f.close()

You can call repeatedly write () to write to the file, but be sure to call f.close () to close the file. When we write a file, the operating system often do not immediately write data to disk, but cached into memory, free time and then slowly write. Only call the close () method, the operating system was to ensure that the data is not written all written to disk. Forget to call close () the consequences of data may be written only part of the disk, and the rest is lost. So, still come with the insurance with the statement:

with open('/Users/michael/test.txt', 'w') as f:
    f.write('Hello, world!')

Character encoding
to be written to a specific encoded text files, to open () function passed encoding parameters will be automatically converted to a string specified encoding.

f = open('/Users/michael/gbk.txt', 'r', encoding='gbk')

Some non-standard coding encounters a file, you may encounter UnicodeDecodeError, because in the text file may be mixed with some of the illegal character encoding. In such cases, open () function also receives a parameter errors, if experience shows how the coding error handling. The easiest way is to simply ignore:

f = open('/Users/michael/gbk.txt', 'r', encoding='gbk', errors='ignore')

When writing to the file 'w' mode, if the file already exists, directly covering (corresponding to a deleted file is newly written). If we want to append to the end of the file how to do? You can pass 'a' is added in order to write (the append) mode.

csv file read and write

Read csv file reader ()

import csv
with open("test.csv","r") as csvfile:
    reader = csv.reader(csvfile)
    #这里不需要readlines
    for line in reader:
        print(line)

Write csv file

import csv

#python2可以用file替代open
with open("test.csv","w") as csvfile: 
    writer = csv.writer(csvfile)

    #先写入columns_name
    writer.writerow(["index","a_name","b_name"])
    #写入多行用writerows
    writer.writerows([[0,1,3],[1,2,3],[2,3,4]])

result:

index a_name b_name
0 1 3
1 2 3
2 3 4

You can also use the file read and write packet pandas

Third, the data type conversion

expression effect
int(x [,base ]) The integer x is converted to a
long(x [,base ]) The converted to a long integer x
float(x ) The transition to a floating point x
complex(real [,imag ]) Creating a complex
str(x ) The object is converted to a string x
repr (x) The string object is converted to an expression x
eval(str ) Python expression for calculating effective in the string, and returns an object
tuple(s ) Converting the sequence s is a tuple
list(s ) Converting the sequence s is a list of
chr(x ) Convert an integer to a character
unichr(x ) Will be converted to an integer Unicode character
words (x) A character into its integer value
hex(x ) Convert an integer to a hexadecimal string
oct(x ) Convert an integer to an octal string

四、 from gensim.models import word2vec

The following warning:

UserWarning: detected Windows; aliasing chunkize to chunkize_serial
  warnings.warn("detected Windows; aliasing chunkize to chunkize_serial")

Solve problems, plus these two lines:

import warnings
warnings.filterwarnings(action = 'ignore', category = UserWarning, module = 'gensim')

Five, Numpy operation

Returns the index of the array element value numpy

import numpy as np
array = np.array([3,5,7,2,8,0,9,45,0])
index = np.argwhere(array == 0)
print(index)

result:

[[5]
 [8]]

Guess you like

Origin blog.csdn.net/qq_39232265/article/details/79183072