अब यहां! मैं "I" शब्द के लिए निम्नलिखित या अगला शब्द खोज रहा हूं। Ex "मैं यहाँ नया हूँ" -> अगला शब्द "am" है।
import re
word = 'i'
with open('tedtalk.txt', 'r') as words:
pat = re.compile(r'\b{}\b \b(\w+)\b'.format(word))
print(pat.findall(words))
with open('tedtalk.txt','r') as f:
for line in f:
phrase = 'I'
if phrase in line:
next(f)
ये वे कोड हैं जिन्हें मैंने अब तक विकसित किया है, लेकिन मैं पहले से ही अटका हुआ हूं। अग्रिम में धन्यवाद!
4 जवाब
data = "This is a test I created. I am blocking the last word issue. I"
search_word = "I"
list_of_words = data.split()
for word_pos, word in enumerate(list_of_words):
if word == search_word and word_pos != len(list_of_words)-1:
print(list_of_words[word_pos+1])
आपके पास 2 विकल्प हैं।
पहले, विभाजन के साथ
with open('tedtalk.txt','r') as f:
data = f.read()
search_word = "I"
list_of_words = data.split()
next_word = list_of_words[list_of_words.index(search_word) + 1]
दूसरा, रेगेक्स के साथ:
import re
regex = re.compile(r"\bI\b\s*?\b(\w+)\b")
with open('tedtalk.txt','r') as f:
data = f.readlines()
result = regex.findall(data)
with open('tedtalk.txt', 'r') as text:
words = text.read().split()
for count, value in enumerate(words):
if value == "I":
print(words[count+1])
आपके पहले कोड में, words
एक फ़ाइल ऑब्जेक्ट है, और लाइन-दर-लाइन सत्यापन में समस्याएँ होंगी। उदाहरण के लिए, निम्नलिखित मामले में, am2
नहीं मिल सकता है।
tedtalk.txt
I am1 new here, I
am2 new here, I am3 new here
इसलिए मैंने प्रोग्राम को संशोधित किया और फ़ाइल को बहुत बड़ा होने से रोकने और मेमोरी को विस्फोट करने से रोकने के लिए कई बार 4096 बाइट्स पढ़े।
डेटा को काट-छाँट करने से रोकने के लिए, I
को एक बार पढ़ने के लिए डेटा के अंत से खोजा जाएगा, और यदि पाया जाता है, तो इसके बाद के डेटा को छोटा कर दिया जाएगा और सामने रख दिया जाएगा। अगले पढ़ने का।
import re
regex = re.compile(r"\bI\b\s*?\b(\w+)\b")
def find_index(data, target_value="I"):
"""Look for spaces from the back, the intention is to find the value between two space blocks and check if it is equal to `target_value`"""
index = once_read_data.rfind(" ")
if index != -1:
index2 = index
while True:
index2 = once_read_data.rfind(" ", 0, index2)
if index2 == -1:
break
t = index - index2
# two adjacent spaces
if t == 1:
continue
elif t == 2 and once_read_data[index2 + 1: index] == target_value:
return index2
result = []
with open('tedtalk.txt', 'r') as f:
# Save data that might have been truncated last time.
prev_data = ""
while True:
once_read_data = prev_data + f.read(4096)
if not once_read_data:
break
index = find_index(once_read_data)
if index is not None:
# Slicing based on the found index.
prev_data = once_read_data[index:]
once_read_data = once_read_data[:index]
else:
prev_data = ""
result += regex.findall(once_read_data)
print(result)
आउटपुट:
['am1', 'am2', 'am3']
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।