line=line.strip("\n")
text = 'a,b,c'
text = text.split(',')
text
# [ 'a', 'b', 'c' ]
text = 'a,b,c'
text = eval('[' + text + ']')
str = "00000003210Runoob01230000000";
print str.strip( '0' ); # 去除首尾字符 0
list1 = ['1', '2', '3']
str1 = ''.join(list1)
x = "Hello World!"
x[2:]
'llo World!'
x[:2]
'He'
x[:-2]
'Hello Worl'
x[-2:]
'd!'
x[2:-2]
'llo Worl'
# variable value as variable name
foo = "bar"
exec(foo + " = 'something else'")
print bar
# something else
There are at least two ways read all lines in a file. First, we can use readlines function. Python’s readlines function reads everything in the text file and has them in a list of lines. Here is how to use Python’s readlines after the opening file.
# Open the file with read only permit
f = open('my_text_file.txt', "r")
# use readlines to read all lines in the file
# The variable "lines" is a list containing all lines in the file
lines = f.readlines()
# close the file after reading the lines.
f.close()
Another way to read lines at once is to simply use
# read all lines at once
lines = list(f)
import sys
f = open(sys.argv[1])
o = open (sys.argv[1]+'.txt', w)
line = f.readline()
n=0
while line:
print(line)
line = f.readline()
o.write(line)
f.close()
#Open the file with read only permit
f = open('my_text_file.txt')
# use readline() to read the first line
line = f.readline()
# use the read line to read further.
# If the file is not empty keep reading one line
# at a time, till the file is empty
while line:
# in python 2+
# print line
# in python 3 print is a builtin function, so
print(line)
# use realine() to read next line
line = f.readline()
f.close()
fh = open('my_text_file.txt')
for line in fh:
# in python 2
# print line
# in python 3
print(line)
fh.close()
# Python code to illustrate the working of strip()
string = ' Geeks for Geeks '
# Leading spaces are removed
print(string.strip())
# Geeks is removed
print(string.strip(' Geeks'))
# Not removed since the spaces do not match
print(string.strip('Geeks'))
cars = ["Ford", "Volvo", "BMW"]
x = len(cars)
for x in cars:
print(x)
cars.append("Honda")
cars.pop(1)
cars.remove("Volvo")
fruits.reverse()
cars.sort()
dict = {'a': 1, 'b': 2, 'b': '3'}
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "dict['Age']: ", dict['Age']
#dict['Age']: 7
dict['Age'] = 8 # 更新
dict['School'] = "RUNOOB" # 添加
del dict['Name'] # 删除键是'Name'的条目
dict.clear() # 清空字典所有条目
del dict # 删除字典
len(dict)
str(dict)
dict.has_key(key)
dict.get(key, default=None)
dict.keys()
dict.items()
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
print(p1.name)
#John
print(p1.age)
#36
p1 = Person("John", 36)
p1.myfunc()
# Hello my name is John
修饰符 | 描述 |
---|---|
re.I | 使匹配对大小写不敏感 |
re.L | 做本地化识别(locale-aware)匹配 |
re.M | 多行匹配,影响 ^ 和 $ |
re.S | 使 . 匹配包括换行在内的所有字符 |
re.U | 根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B. |
re.X | 该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。 |
import re
xx = "guru99,education is fun"
r1 = re.findall(r"^\w+", xx)
print((re.split(r'\s','we are splitting the words')))
print((re.split(r's','split the words')))
re.search 扫描整个字符串并返回第一个成功的匹配。
#!/usr/bin/python
import re
line = "ATGCATCGTATCATCGATCGATCGATCG";
searchObj = re.search( r'ATCG', line)
if searchObj:
print "searchObj.group() : ", searchObj.group()
else:
print "Nothing found!!"
#!/usr/bin/python
import re
line = "Cats are smarter than dogs";
searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)
if searchObj:
print "searchObj.group() : ", searchObj.group()
print "searchObj.group(1) : ", searchObj.group(1)
print "searchObj.group(2) : ", searchObj.group(2)
else:
print "Nothing found!!"
# searchObj.group() : Cats are smarter than dogs
# searchObj.group(1) : Cats
# searchObj.group(2) : smarter
patterns = ['software testing', 'guru99']
text = 'software testing is fun?'
for pattern in patterns:
print('Looking for "%s" in "%s" ->' % (pattern, text))
if re.search(pattern, text):
print('found a match!')
else:
print('no match')
在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
# -*- coding:UTF8 -*-
import re
pattern = re.compile(r'\d+') # 查找数字
result1 = pattern.findall('runoob 123 google 456')
result2 = pattern.findall('run88oob123google456', 0, 10)
print(result1)
print(result2)
# ['123', '456']
# ['88', '12']
和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并把它们作为一个迭代器返回。
# -*- coding: UTF-8 -*-
import re
it = re.finditer(r"\d+","12a32bc43jf3")
for match in it:
print (match.group() )
12
32
43
3
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。
The match function is used to match the RE pattern to string with optional flags. In this method, the expression "w+" and "\W" will match the words starting with letter 'g' and thereafter, anything which is not started with 'g' is not identified. To check match for each element in the list or string, we run the forloop.
import re
list = ["guru99 get", "guru99 give", "guru Selenium"]
for element in list:
z = re.match("(g\w+)\W(g\w+)", element)
print((z.groups()))
print(re.match('www', 'www.runoob.com').span())
#(0, 3)
print(re.match('www', 'www.runoob.com').group())
# www
abc = 'guru99@google.com
, careerguru99@hotmail.com, users@yahoomail.com'
emails = re.findall(r'[\w\.-]+@[\w\.-]+', abc)
for email in emails:
print(email)
Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element of the list.
re.split(r'\W+', 'Words, words, words.')
# ['Words', 'words', 'words', '']
re.split(r'(\W+)', 'Words, words, words.')
# ['Words', ', ', 'words', ', ', 'words', '.', '']
re.split(r'\W+', 'Words, words, words.', 1)
# ['Words', 'words, words.']
re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)
# ['0', '3', '9']
#!/usr/bin/python
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);
import os
os.system('[ -d split_file ] || mkdir split_file ')