Python 外卡赛
Python wild card match
我有这样一个文件
iPhone6-16GB-Black,40000,10000,10000,20000
iPhone6-16GB-White,40000,10000,10000,20000
iPhone6-16GB-Gold,40000,10000,10000,20000
iPhone6-16GB-Silver,40000,10000,10000,20000
iPhone6-16GB-Gray,40000,10000,10000,20000
iPhone6-64GB-Black,40000,10000,10000,20000
iPhone6-64GB-White,40000,10000,10000,20000
我需要逐行搜索并找到与输入匹配的所有行
if input = iPhone6-*-* 它应该匹配所有带有 iPhone6-
的行
if input = iPhone6-16GB-* 它应该匹配所有带有 iPhone6-16GB-
的行
if input = *-*-* 它应该匹配所有行
到目前为止我有这样的代码
for line in devLines:
line = line.rstrip()
line = line.strip()
if line and not line.startswith("#"):
devName = line.split(",")[0]
devName = devName.strip()
if re.search(device, line) :
我想这就是你的意思吧?请更具体。
我使用的文件。 (test.txt)
name-12-100
name-12-200
name-24-100
name-36-100
代码。
from re import search
ipt = 'name-12'
with open('test.txt','r') as f:
for line in f.readlines():
if search(ipt, line):
print line
'name'
name-12-100
name-12-200
name-24-100
name-36-100
'name-12'
name-12-100
name-12-200
'name-12-200'
name-12-200
- 获取用户的搜索输入。
- 通过
csv
模块打开并reader文件。
- 用搜索文本检查每行的第一项。
- 打印结果。
代码:
import csv
search_text = raw_input('Enter search text:').strip()
file2 = '/home/vivek/Desktop/Whosebug/file3.txt'
with open(file2) as fp1:
root = csv.reader(fp1)
if search_text=="*":
result = list(root)
else:
result = [i for i in root if search_text==i[0]]
#result = []
#for i in root:
# if search_text == i[0]:
# result.append(i)
print result
输出:
Enter search text:*
[['iPhone6-16GB-Black', '40000', '10000', '10000', '20000'], ['iPhone6-16GB-White', '40000', '10000', '10000', '20000'], ['iPhone6-16GB-Gold', '40000', '10000', '10000', '20000'], ['iPhone6-16GB-Silver', '40000', '10000', '10000', '20000'], ['iPhone6-16GB-Gray', '40000', '10000', '10000', '20000'], ['iPhone6-64GB-Black', '40000', '10000', '10000', '20000'], ['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:iPhone6-16GB-Gray
[['iPhone6-16GB-Gray', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:test
[]
vivek@vivek:~/Desktop/Whosebug$
通过正则表达式并根据问题修改:
创建搜索文本的正则表达式。
代码:
import csv
import re
import pprint
file2 = '/home/vivek/Desktop/Whosebug/file3.txt'
def searchText(search_text):
print "search_text:", search_text
with open(file2) as fp1:
root = csv.reader(fp1)
result = [i for i in root if re.findall(search_text, i[0])]
return result
search_text = raw_input('Enter search text:').strip()
#validate input
try:
tmp1, tmp2, tmp3 = search_text.split("-")
search_text = search_text.replace("*", "[^-]*")
result = searchText(search_text)
pprint.pprint(result)
except:
print "wrong search text. *-*-*"
输出:
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:*-*-*
search_text: [^-]*-[^-]*-[^-]*
[['iPhone6-16GB-Black', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-White', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Gold', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Silver', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Gray', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-Black', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:iPhone6-*-*
search_text: iPhone6-[^-]*-[^-]*
[['iPhone6-16GB-Black', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-White', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Gold', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Silver', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Gray', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-Black', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:iPhone6-64GB-*
search_text: iPhone6-64GB-[^-]*
[['iPhone6-64GB-Black', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:iPhone6-64GB-White
search_text: iPhone6-64GB-White
[['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:iPhone6-*-White
search_text: iPhone6-[^-]*-White
[['iPhone6-16GB-White', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$
您可以使用 fnmatch.translate
,它将 shell 当作通配符,并将它们转换为可与 re.compile
一起使用的正则表达式字符串,以创建可用于过滤您的匹配器结果:
import re
from fnmatch import translate as wc_to_re
search_for = raw_input('Search for: ') + '*'
is_match = re.compile(wc_to_re(search_for), flags=re.I).match
with open('yourfile') as fin:
for line in filter(is_match, fin):
print line, # or do something else appropriate?
匹配不区分大小写,因此输入:iphone6-16gb*
将打印以下内容:
iPhone6-16GB-Black,40000,10000,10000,20000
iPhone6-16GB-White,40000,10000,10000,20000
iPhone6-16GB-Gold,40000,10000,10000,20000
iPhone6-16GB-Silver,40000,10000,10000,20000
iPhone6-16GB-Gray,40000,10000,10000,20000
而 iphone6-16gb-g*
将给出:
iPhone6-16GB-Gold,40000,10000,10000,20000
iPhone6-16GB-Gray,40000,10000,10000,20000
我有这样一个文件
iPhone6-16GB-Black,40000,10000,10000,20000
iPhone6-16GB-White,40000,10000,10000,20000
iPhone6-16GB-Gold,40000,10000,10000,20000
iPhone6-16GB-Silver,40000,10000,10000,20000
iPhone6-16GB-Gray,40000,10000,10000,20000
iPhone6-64GB-Black,40000,10000,10000,20000
iPhone6-64GB-White,40000,10000,10000,20000
我需要逐行搜索并找到与输入匹配的所有行
if input = iPhone6-*-* 它应该匹配所有带有 iPhone6-
的行if input = iPhone6-16GB-* 它应该匹配所有带有 iPhone6-16GB-
的行if input = *-*-* 它应该匹配所有行
到目前为止我有这样的代码
for line in devLines:
line = line.rstrip()
line = line.strip()
if line and not line.startswith("#"):
devName = line.split(",")[0]
devName = devName.strip()
if re.search(device, line) :
我想这就是你的意思吧?请更具体。
我使用的文件。 (test.txt)
name-12-100
name-12-200
name-24-100
name-36-100
代码。
from re import search
ipt = 'name-12'
with open('test.txt','r') as f:
for line in f.readlines():
if search(ipt, line):
print line
'name'
name-12-100
name-12-200
name-24-100
name-36-100
'name-12'
name-12-100
name-12-200
'name-12-200'
name-12-200
- 获取用户的搜索输入。
- 通过
csv
模块打开并reader文件。 - 用搜索文本检查每行的第一项。
- 打印结果。
代码:
import csv
search_text = raw_input('Enter search text:').strip()
file2 = '/home/vivek/Desktop/Whosebug/file3.txt'
with open(file2) as fp1:
root = csv.reader(fp1)
if search_text=="*":
result = list(root)
else:
result = [i for i in root if search_text==i[0]]
#result = []
#for i in root:
# if search_text == i[0]:
# result.append(i)
print result
输出:
Enter search text:*
[['iPhone6-16GB-Black', '40000', '10000', '10000', '20000'], ['iPhone6-16GB-White', '40000', '10000', '10000', '20000'], ['iPhone6-16GB-Gold', '40000', '10000', '10000', '20000'], ['iPhone6-16GB-Silver', '40000', '10000', '10000', '20000'], ['iPhone6-16GB-Gray', '40000', '10000', '10000', '20000'], ['iPhone6-64GB-Black', '40000', '10000', '10000', '20000'], ['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:iPhone6-16GB-Gray
[['iPhone6-16GB-Gray', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:test
[]
vivek@vivek:~/Desktop/Whosebug$
通过正则表达式并根据问题修改:
创建搜索文本的正则表达式。
代码:
import csv
import re
import pprint
file2 = '/home/vivek/Desktop/Whosebug/file3.txt'
def searchText(search_text):
print "search_text:", search_text
with open(file2) as fp1:
root = csv.reader(fp1)
result = [i for i in root if re.findall(search_text, i[0])]
return result
search_text = raw_input('Enter search text:').strip()
#validate input
try:
tmp1, tmp2, tmp3 = search_text.split("-")
search_text = search_text.replace("*", "[^-]*")
result = searchText(search_text)
pprint.pprint(result)
except:
print "wrong search text. *-*-*"
输出:
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:*-*-*
search_text: [^-]*-[^-]*-[^-]*
[['iPhone6-16GB-Black', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-White', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Gold', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Silver', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Gray', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-Black', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:iPhone6-*-*
search_text: iPhone6-[^-]*-[^-]*
[['iPhone6-16GB-Black', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-White', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Gold', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Silver', '40000', '10000', '10000', '20000'],
['iPhone6-16GB-Gray', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-Black', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:iPhone6-64GB-*
search_text: iPhone6-64GB-[^-]*
[['iPhone6-64GB-Black', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:iPhone6-64GB-White
search_text: iPhone6-64GB-White
[['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$ python 30.py
Enter search text:iPhone6-*-White
search_text: iPhone6-[^-]*-White
[['iPhone6-16GB-White', '40000', '10000', '10000', '20000'],
['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/Whosebug$
您可以使用 fnmatch.translate
,它将 shell 当作通配符,并将它们转换为可与 re.compile
一起使用的正则表达式字符串,以创建可用于过滤您的匹配器结果:
import re
from fnmatch import translate as wc_to_re
search_for = raw_input('Search for: ') + '*'
is_match = re.compile(wc_to_re(search_for), flags=re.I).match
with open('yourfile') as fin:
for line in filter(is_match, fin):
print line, # or do something else appropriate?
匹配不区分大小写,因此输入:iphone6-16gb*
将打印以下内容:
iPhone6-16GB-Black,40000,10000,10000,20000
iPhone6-16GB-White,40000,10000,10000,20000
iPhone6-16GB-Gold,40000,10000,10000,20000
iPhone6-16GB-Silver,40000,10000,10000,20000
iPhone6-16GB-Gray,40000,10000,10000,20000
而 iphone6-16gb-g*
将给出:
iPhone6-16GB-Gold,40000,10000,10000,20000
iPhone6-16GB-Gray,40000,10000,10000,20000