简体   繁体   English

Python:我可以在正则表达式上使用吗

[英]Python: Can I use on the regular expression

I tried to do some work on with regular expression but, I had difficulties as there are lots of list of cars. 我试图用正则表达式做一些工作,但是由于有很多汽车清单,所以我遇到了困难。 The line of code will longer if I used the below code 如果我使用以下代码,则代码行会更长

car_name = re.compile(r'.*(?=\n\nBMW I3\s*?\n)')
car_name = re.compile(r'.*(?=\n\nBMW I8\s*?\n)')
car_name = re.compile(r'.*(?=\n\nBMW 320d\s*?\n)')
...

So, I created a CSV file that contains a car list and get the list as following code: 因此,我创建了一个包含汽车列表的CSV文件,并通过以下代码获取该列表:

car_df = pd.read_csv("my path")

I wonder how can I use the csv file applied to the regular expression Is ther any way to bring a list of cars and apply on the regular expression? 我想知道如何使用应用于正则表达式的csv文件吗?还有什么方法可以带来汽车列表并应用于正则表达式?

Try this : 尝试这个 :

import re

bmw_cars = []
for car in car_list:
   if re.search('.*BMW.*',car):
      bmw_cars.append(car)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM