我正在尝试使用python regex从字符串中提取评论编号(数字)。该代码有效,但是只要我添加了if,只要稍加修改regex公式,就可以为每种可能情况添加else语句。 是否有更好的方法来使用单个公式,而不是为每种可能的情况不断更改公式?
这是我的代码吗?
import re
# This below is the list containing the tuples with the data I want to extract from.
data_tup = [('B079T2F3CY', "SponsoredThese are ads for products you'll find on Amazon.com.Clicking an ad will take you to the product's page.Learn more about Sponsored Products.Star Wars Poster Inspired Watercolor Wall Art Jedi Yoda Death Star Prints Decor Paper Set of 6 8x10 P49 by PGbureau4.8 out of 5 stars16$24.99$24.99Eligible for Shipping to United Arab Emirates"),
('B01A0NB55A', 'Star Wars Mug, Lightsabers Appear With Heat (12 oz)4.3 out of 5 stars948$9.99$9.99Eligible for Shipping to United Arab EmiratesMore Buying Choices$6.91(2 used & new offers)'),
('B00VF18WWG', 'Star Wars: The Phantom MenaceMPAA Rating: PG (Parental Guidance Suggested)|Closed Caption4.2 out of 5 stars3,037Prime Videofrom$19.99$19.99to buyStarring:Liam Neeson,Ewan McGregor,Natalie PortmanandJake LloydDirected by:George LucasRuntime:136 minutes'),
('B00KRIR2HY', "Star Wars Men's Words Of Wisdom T-Shirt4.6 out of 5 stars372$11.00$11.00-$25.99$25.99"),
('B07CFYL578', 'VTech Star Wars First Order Stormtrooper Smartwatch4.2 out of 5 stars191$53.48$53.48Eligible for Shipping to United Arab EmiratesMore Buying Choices$30.45(5 used & new offers)Ages: 5 - 9 years'),
('B077T5MG5F', 'Star Wars: The Last Jedi (Theatrical Version)MPAA Rating: PG-13 (Parents Strongly Cautioned)|Closed Caption3.8 out of 5 stars4,738Prime Videofrom$2.99$2.99to rentStarring:Oscar Isaac,Mark Hamill,Daisy RidleyandJohn BoyegaDirected by:Rian JohnsonRuntime:151 minutes'),
('B079T2F3CY', 'Star Wars Poster Inspired Watercolor Wall Art Jedi Yoda Death Star Prints Decor Paper Set of 6 8x10 P49 by PGbureau4.8 out of 5 stars16$24.99$24.99Eligible for Shipping to United Arab Emirates'),
('B00VF0M7QE', 'Star Wars: Return of the Jedi (Theatrical Version)MPAA Rating: PG (Parental Guidance Suggested)|Closed Caption4.5 out of 5 stars1,055Prime Videofrom$19.99$19.99to buyStarring:Mark Hamill,Harrison Ford,Carrie Fisher, et al.Directed by:Richard MarquandRuntime:134 minutes'),
('B01J5GKX60', 'Star Wars Classic Space Battle Full Sheet Set4.5 out of 5 stars53$34.99$34.99Eligible for Shipping to United Arab EmiratesOnly 2 left in stock - order soon.More Buying Choices$26.99(3 new offers)'),
('B079MB31DY', "SponsoredThese are ads for products you'll find on Amazon.com.Clicking an ad will take you to the product's page.Learn more about Sponsored Products.Enjoy The Wood Star Wars Music Box Wooden Star Wars Fans Custom Gift for Boyfriend Gift for Brother4.9 out of 5 stars22$19.99$19.99Eligible for Shipping to United Arab Emirates"),
('B00ZYXVU7K', "SponsoredThese are ads for products you'll find on Amazon.com.Clicking an ad will take you to the product's page.Learn more about Sponsored Products.Star Wars Lightsaber Heat Change Mug4.1 out of 5 stars158$13.95$13.95Eligible for Shipping to United Arab EmiratesOnly 9 left in stock - order soon."),
('B014HPF5G2', 'Hasbro Gaming Star Wars Bop It Game4.7 out of 5 stars446$14.99$14.99$16.99$16.99Eligible for Shipping to United Arab EmiratesMore Buying Choices$7.99(16 used & new offers)Ages: 8 years and up'),
('B00VN0DLRA', 'Star Wars: A New HopeMPAA Rating: PG (Parental Guidance Suggested)|Closed Caption4.5 out of 5 stars2,226Prime Videofrom$19.99$19.99to buyStarring:Mark Hamill,Harrison Ford,Carrie FisherandPeter CushingDirected by:George LucasRuntime:124 minutes'),
('B079MB31DY', 'Enjoy The Wood Star Wars Music Box Wooden Star Wars Fans Custom Gift for Boyfriend Gift for Brother4.9 out of 5 stars22$19.99$19.99Eligible for Shipping to United Arab Emirates'),
('B076FDK9TF', 'Lenovo Star Wars: Jedi Challenges, Smartphone Powered Augmented Reality ExperienceDec 1, 2017|by Lenovo4.0 out of 5 stars102iOS$64.99$64.99$99.99$99.99Eligible for Shipping to United Arab EmiratesMore Buying Choices$35.99(35 used & new offers)'),
('B015NFSC24', "Star Wars Classic Logo and Tie Fighter Men's Short Sleeve T-Shirt4.8 out of 5 stars52$15.89$15.89-$19.99$19.99"),
('B013GTX6JI', 'Star Wars - The Complete Saga Episodes I-VI4.5 out of 5 stars6,958Blu-ray$78.49$78.49More Buying Choices$45.99(26 used & new offers)DVD$43.93$43.93In stock on August 30, 2019.More Buying Choices$43.00(11 used & new offers)'),
('B013C1JLLM', 'Star Wars Episode VII Plastic Table Cover, Party Favor4.5 out of 5 stars8More Buying Choices$1.59(16 new offers)Ages: 36 months - 15 years'),
('0440418410', 'The Boys Start the War (Boy/Girl Battle)byPhyllis Reynolds Naylor|Jan 8, 20024.2 out of 5 stars37Paperback$6.99$6.99More Buying Choices$0.10(122 used & new offers)Kindle$6.99$6.99Library Binding$17.80$17.80Only 3 left in stock (more on the way).More Buying Choices$5.62(47 used & new offers)'),
('B00CLS39UM', 'SponsoredThese are ads for products you\'ll find on Amazon.com.Clicking an ad will take you to the product\'s page.Learn more about Sponsored Products.Star Wars"Who\'s Your Daddy"? Father\'s Day Coffee Mug Collectible Novelty 11 Oz Nice Valentine Inspirational and Motivational Souvenir4.7 out of 5 stars143$10.99$10.99Eligible for Shipping to United Arab Emirates'),
('B018SAMUE2', "SponsoredThese are ads for products you'll find on Amazon.com.Clicking an ad will take you to the product's page.Learn more about Sponsored Products.Star Wars Grid Mug of Episode VII + 4 Free Bonus The Force Awakens Stickers4.1 out of 5 stars3$14.99$14.99Eligible for Shipping to United Arab EmiratesOnly 12 left in stock - order soon."),
('B00O0TE4MC', "SponsoredThese are ads for products you'll find on Amazon.com.Clicking an ad will take you to the product's page.Learn more about Sponsored Products.Star Wars Classic 63” Drapery /Curtain 4pc Set (2 Panels, 2 Tie backs) - R2D2, C3PO, Chewbacca, Darth Vader, Stormtrooper - Official Star Wars Product4.5 out of 5 stars168$13.01$13.01$19.99$19.99Eligible for Shipping to United Arab Emirates")]
# This is the actual function that I am currently using:
def reviews_extractor(data):
reviews = re.findall("[stars][\d,]+\$",data[1])
if reviews == []:
reviews = re.findall("[s][\W\d,]+[P]", data[1])
if reviews == []:
reviews = re.findall("[s][\W\d,]+[i]", data[1])
if reviews == []:
reviews = re.findall("[s][\W\d,]+[B]", data[1])
if reviews == []:
reviews = re.findall("[s][\W\d,]+[M]", data[1])
if reviews == []:
print('Empty')
else:
print(reviews[0][1:-1])
else:
print(reviews[0][1:-1])
else:
print(reviews[0][1:-1])
else:
print(reviews[0][1:-1])
else:
print(reviews[0][1:-1])
for line in data_tup:
reviews_extractor(line)
有没有一种更好的方法可以将所有这些东西都放进去?
答案 0 :(得分:1)
您要提取客户评论的数量吗?
def reviews_extractor(data):
# capturing a digit after `stars` e.g. stars5
print(re.search(r'stars([0-9.,]*)', data[1]).group(1))
答案 1 :(得分:0)
在这里,我假设每个评论编号都以关键字“ stars”开头,以“ $”结尾
for d in data_tup:
m = re.compile('stars(.*?)\$', re.DOTALL).search(d[1])
f = m.group(1)
output = ''.join(c for c in f if c.isdigit())
print(output)
答案 2 :(得分:0)
如果您想要星号之后的数字,则此正则表达式应该起作用:"\d(?:.\d)? out of 5 stars(\d+)"
import re
data_tup = [('B079T2F3CY', "SponsoredThese are ads for products you'll find on Amazon.com.Clicking an ad will take you to the product's page.Learn more about Sponsored Products.Star Wars Poster Inspired Watercolor Wall Art Jedi Yoda Death Star Prints Decor Paper Set of 6 8x10 P49 by PGbureau4.8 out of 5 stars16$24.99$24.99Eligible for Shipping to United Arab Emirates"),
('B01A0NB55A', 'Star Wars Mug, Lightsabers Appear With Heat (12 oz)4.3 out of 5 stars948$9.99$9.99Eligible for Shipping to United Arab EmiratesMore Buying Choices$6.91(2 used & new offers)'),
('B00VF18WWG', 'Star Wars: The Phantom MenaceMPAA Rating: PG (Parental Guidance Suggested)|Closed Caption4.2 out of 5 stars3,037Prime Videofrom$19.99$19.99to buyStarring:Liam Neeson,Ewan McGregor,Natalie PortmanandJake LloydDirected by:George LucasRuntime:136 minutes'),
('B00KRIR2HY', "Star Wars Men's Words Of Wisdom T-Shirt4.6 out of 5 stars372$11.00$11.00-$25.99$25.99"),
('B07CFYL578', 'VTech Star Wars First Order Stormtrooper Smartwatch4.2 out of 5 stars191$53.48$53.48Eligible for Shipping to United Arab EmiratesMore Buying Choices$30.45(5 used & new offers)Ages: 5 - 9 years'),
('B077T5MG5F', 'Star Wars: The Last Jedi (Theatrical Version)MPAA Rating: PG-13 (Parents Strongly Cautioned)|Closed Caption3.8 out of 5 stars4,738Prime Videofrom$2.99$2.99to rentStarring:Oscar Isaac,Mark Hamill,Daisy RidleyandJohn BoyegaDirected by:Rian JohnsonRuntime:151 minutes'),
('B079T2F3CY', 'Star Wars Poster Inspired Watercolor Wall Art Jedi Yoda Death Star Prints Decor Paper Set of 6 8x10 P49 by PGbureau4.8 out of 5 stars16$24.99$24.99Eligible for Shipping to United Arab Emirates'),
('B00VF0M7QE', 'Star Wars: Return of the Jedi (Theatrical Version)MPAA Rating: PG (Parental Guidance Suggested)|Closed Caption4.5 out of 5 stars1,055Prime Videofrom$19.99$19.99to buyStarring:Mark Hamill,Harrison Ford,Carrie Fisher, et al.Directed by:Richard MarquandRuntime:134 minutes'),
('B01J5GKX60', 'Star Wars Classic Space Battle Full Sheet Set4.5 out of 5 stars53$34.99$34.99Eligible for Shipping to United Arab EmiratesOnly 2 left in stock - order soon.More Buying Choices$26.99(3 new offers)'),
('B079MB31DY', "SponsoredThese are ads for products you'll find on Amazon.com.Clicking an ad will take you to the product's page.Learn more about Sponsored Products.Enjoy The Wood Star Wars Music Box Wooden Star Wars Fans Custom Gift for Boyfriend Gift for Brother4.9 out of 5 stars22$19.99$19.99Eligible for Shipping to United Arab Emirates"),
('B00ZYXVU7K', "SponsoredThese are ads for products you'll find on Amazon.com.Clicking an ad will take you to the product's page.Learn more about Sponsored Products.Star Wars Lightsaber Heat Change Mug4.1 out of 5 stars158$13.95$13.95Eligible for Shipping to United Arab EmiratesOnly 9 left in stock - order soon."),
('B014HPF5G2', 'Hasbro Gaming Star Wars Bop It Game4.7 out of 5 stars446$14.99$14.99$16.99$16.99Eligible for Shipping to United Arab EmiratesMore Buying Choices$7.99(16 used & new offers)Ages: 8 years and up'),
('B00VN0DLRA', 'Star Wars: A New HopeMPAA Rating: PG (Parental Guidance Suggested)|Closed Caption4.5 out of 5 stars2,226Prime Videofrom$19.99$19.99to buyStarring:Mark Hamill,Harrison Ford,Carrie FisherandPeter CushingDirected by:George LucasRuntime:124 minutes'),
('B079MB31DY', 'Enjoy The Wood Star Wars Music Box Wooden Star Wars Fans Custom Gift for Boyfriend Gift for Brother4.9 out of 5 stars22$19.99$19.99Eligible for Shipping to United Arab Emirates'),
('B076FDK9TF', 'Lenovo Star Wars: Jedi Challenges, Smartphone Powered Augmented Reality ExperienceDec 1, 2017|by Lenovo4.0 out of 5 stars102iOS$64.99$64.99$99.99$99.99Eligible for Shipping to United Arab EmiratesMore Buying Choices$35.99(35 used & new offers)'),
('B015NFSC24', "Star Wars Classic Logo and Tie Fighter Men's Short Sleeve T-Shirt4.8 out of 5 stars52$15.89$15.89-$19.99$19.99"),
('B013GTX6JI', 'Star Wars - The Complete Saga Episodes I-VI4.5 out of 5 stars6,958Blu-ray$78.49$78.49More Buying Choices$45.99(26 used & new offers)DVD$43.93$43.93In stock on August 30, 2019.More Buying Choices$43.00(11 used & new offers)'),
('B013C1JLLM', 'Star Wars Episode VII Plastic Table Cover, Party Favor4.5 out of 5 stars8More Buying Choices$1.59(16 new offers)Ages: 36 months - 15 years'),
('0440418410', 'The Boys Start the War (Boy/Girl Battle)byPhyllis Reynolds Naylor|Jan 8, 20024.2 out of 5 stars37Paperback$6.99$6.99More Buying Choices$0.10(122 used & new offers)Kindle$6.99$6.99Library Binding$17.80$17.80Only 3 left in stock (more on the way).More Buying Choices$5.62(47 used & new offers)'),
('B00CLS39UM', 'SponsoredThese are ads for products you\'ll find on Amazon.com.Clicking an ad will take you to the product\'s page.Learn more about Sponsored Products.Star Wars"Who\'s Your Daddy"? Father\'s Day Coffee Mug Collectible Novelty 11 Oz Nice Valentine Inspirational and Motivational Souvenir4.7 out of 5 stars143$10.99$10.99Eligible for Shipping to United Arab Emirates'),
('B018SAMUE2', "SponsoredThese are ads for products you'll find on Amazon.com.Clicking an ad will take you to the product's page.Learn more about Sponsored Products.Star Wars Grid Mug of Episode VII + 4 Free Bonus The Force Awakens Stickers4.1 out of 5 stars3$14.99$14.99Eligible for Shipping to United Arab EmiratesOnly 12 left in stock - order soon."),
('B00O0TE4MC', "SponsoredThese are ads for products you'll find on Amazon.com.Clicking an ad will take you to the product's page.Learn more about Sponsored Products.Star Wars Classic 63” Drapery /Curtain 4pc Set (2 Panels, 2 Tie backs) - R2D2, C3PO, Chewbacca, Darth Vader, Stormtrooper - Official Star Wars Product4.5 out of 5 stars168$13.01$13.01$19.99$19.99Eligible for Shipping to United Arab Emirates")]
for code, text in data_tup:
print(re.search("\d(?:.\d)? out of 5 stars(\d+)", text).group(1))
正则表达式细目:
\d(?:.\d)?
:
\d
:任何十进制字符(0-9)。(?:.\d)?
:.\d
表示文字.
后跟十进制字符(0-9),(?: )
告诉正则表达式将所有字符分组,但不存储子字符串(以便我们可以应用?
而不存储内容),最后一个?
可以是零次或一次。out of 5 stars
:确切的文字。(\d+)
:\d
表示任何十进制字符(0-9),+
表示其中的1个或多个,并且()
告诉正则表达式将所有字符并存储此子字符串。答案 3 :(得分:0)
@joumaico中有一个小错误,如果该数字后面有逗号,则会出现错误。例如“满分5星,948美元,Prime $ 9”将引发错误。
更通用的正则表达式是:
def reviews_extractor(data):
# Only captures digit, (all length of digits)
return (re.search(r'stars([0-9]+(,[0-9]+)*)', data[1]).group(1))