我有一个像这样的文件
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
我需要逐行搜索并找到与输入匹配的所有行
如果输入= iPhone6 - * - *它应匹配iPhone6的所有行 -
如果输入= iPhone6-16GB- *它应匹配iPhone6-16GB的所有行 -
如果输入= * - * - *它应匹配所有行
到目前为止,我有这样的代码
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) :
答案 0 :(得分:1)
您可以使用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
答案 1 :(得分:0)
我认为这就是你的意思?请更具体一点。
我使用的文件。 (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-12-100
name-12-200
name-24-100
name-36-100
'名称-12'
name-12-100
name-12-200
'名称-12-200'
name-12-200
答案 2 :(得分:0)
csv
模块打开和读取文件。代码:
import csv
search_text = raw_input('Enter search text:').strip()
file2 = '/home/vivek/Desktop/stackoverflow/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/stackoverflow$ python 30.py
Enter search text:iPhone6-16GB-Gray
[['iPhone6-16GB-Gray', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/stackoverflow$ python 30.py
Enter search text:test
[]
vivek@vivek:~/Desktop/stackoverflow$
通过正则表达式并根据问题的修改。:
创建搜索文本的正则表达式。
代码:
import csv
import re
import pprint
file2 = '/home/vivek/Desktop/stackoverflow/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/stackoverflow$ 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/stackoverflow$ 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/stackoverflow$ 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/stackoverflow$ python 30.py
Enter search text:iPhone6-64GB-White
search_text: iPhone6-64GB-White
[['iPhone6-64GB-White', '40000', '10000', '10000', '20000']]
vivek@vivek:~/Desktop/stackoverflow$ 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/stackoverflow$