[Python] I have a question about sentence search and saving!

Asked 1 years ago, Updated 1 years ago, 115 views

Assume that spring="summerfallautumnwinter" is in the text file.

a = spring="summerfallautumnwinter"

In this part a = spring = (English sentence) Summer fall early autumn winter" Except for spring and quotation marks (English sentence), summer, fall, early fall, winter How can I save this part separately?

python regex

2022-09-22 20:58

2 Answers

I was going to answer what you posted earlier, but you deleted it.

import re

case = re.compile(r'data-src=".+?"') #data-src=" to find strings that begin and end with ". 
html = 'data-src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTADNXd4lOirlB2FGUCdEmI1h6YEiHWDgQUutou3BshqBgiG_kW" 640 × 436 - kwang82.hankyung.com data-src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcS7orm2JDZH2B1mlD4mXfd6rMfUABuYDGMAf9P16-jLnOdhwFRJ"'
links = case.findall(html) # A function that returns all strings corresponding to the rule as a list.
i = 0
while True:
    if i == len(links):
        break
    links[i] = links[i][10:-1] # Delete the found strings because they contain data-src" and "".
    i += 1
for link in links:
    print(link)


2022-09-22 20:58

You can also use search.

http://hashcode.co.kr/questions/3560/ Regular Expression-Example-Question-In Sentences-Specific-Word-Exclusion-Extraction It's the same as that.

import re

search_target = 'a = spring="summerfallautumnwinter"'
regex = 'spring="(.*)"'
r = re.search(regex, search_target)

if r:
    print(r.group(1))

Learn the regular expressions when handling strings.


2022-09-22 20:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.