1
votes

Comment extraire des chiffres séparés par un point suivi d'un ensemble spécifique de mots regex python?

J'essaie d'extraire le nombre "4.3" de l'exemple de chaîne suivant qui est toujours suivi par cet ensemble de mots: "sur 5 étoiles":

3.8
4.8
4.5
4.5
4.9
4.1
4.7
4.9
4.0
4.8

Comment puis-je extraire les avec regex?

Voici mon code:

<re.Match object; span=(111, 125), match='out of 5 stars'>
<re.Match object; span=(119, 133), match='out of 5 stars'>
<re.Match object; span=(114, 128), match='out of 5 stars'>
<re.Match object; span=(49, 63), match='out of 5 stars'>
<re.Match object; span=(252, 266), match='out of 5 stars'>
<re.Match object; span=(189, 203), match='out of 5 stars'>
<re.Match object; span=(39, 53), match='out of 5 stars'>
<re.Match object; span=(86, 100), match='out of 5 stars'>
<re.Match object; span=(103, 117), match='out of 5 stars'>
<re.Match object; span=(107, 121), match='out of 5 stars'>
<re.Match object; span=(69, 83), match='out of 5 stars'>

Cependant j'obtiens ces résultats:

import re
data_tup = [('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")]

for tup in data_tup:
    number_of_stars = re.search(r'([0-9.,]*)out of 5 stars',tup[1])
    print(number_of_stars)

Voici ce que j'aimerais obtenir:

('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)')


1 commentaires

re.findall ("(\ d \ S +) \ s * out of 5 stars", ''. join (sum (data_tup, ())))


3 Réponses :


2
votes

Cet objet de correspondance contient un tas de choses. La documentation se trouve sur https://docs.python.org/3/library /re.html#match-objects

Voici la solution:

import re
data_tup = [('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")]

for tup in data_tup:
    number_of_stars = re.search(r'([0-9.,]*) out of 5 stars', tup[1]).group(1)
    print(number_of_stars)

L'appel de .group (0) vous donnerait toute la partie correspondante de la chaîne (par exemple, 3,8 étoiles sur 5 ). L'appel de .group (1) vous donne juste le contenu de ce qui correspond à l'expression dans le premier ensemble de parenthèses. Aussi, mettez un espace avant le mot "out", vous ne voulez pas que cet espace fasse partie du nombre que vous extrayez.


0 commentaires

2
votes
re.search('([0-9.,]*) out of 5 stars',tup[1]).group(1)

0 commentaires

2
votes
for tup in data_tup:
    number_of_stars = re.search(r'(\d.\d) out of 5 stars', tup[1]).group(1)
    print(number_of_stars)

use .group(1) to extract the content in the first parentheses
if you use group(0) or group(), it will return the whole string matched.
\d is equivalent to [0-9]
remember to add an empty space before "out of 5 stars"
the type of the extracted data would be string, cast it to float if you want to compute it.

0 commentaires