If I have this string, it contains several lines separated by newlines.
data_string = """
te gustan los animales?
puede que algunos me gusten pero no se mucho de eso
animales
puede que algunos me gusten pero no se mucho de eso
te gustan las plantas?
no se mucho sobre ellas
carnivoro
segun mis registros son aquellos que se alimentan de carne
"""
# Split the string into individual lines
lines = data_string.splitlines()
# Open the check.py file for writing
with open("check.py", "w") as f:
# Write the lines in the file here...
for line in lines:
#How to write the code with this lines info?
Using python how can I create a file using the content of the lines inside the data_string
variable so that it is thus inside a new file.
This is how the resulting file should look like, note that the lines "te gustan los animales?"
, "animales"
, "te gustan las plantas?"
and "carnivoro"
were left as parameters of SequenceMatcher(None, str1, "here").ratio()
and the lines "puede que algunos me gusten pero no se mucho de eso"
, "no se mucho sobre ellas"
, "no se mucho sobre ellas"
and "segun mis registros son aquellos que se alimentan de carne"
were left as response_text = "here"
Output file called check.py
:
from difflib import SequenceMatcher
def check_function(str1):
similarity_ratio = 0.0
response_text = "no coincide con nada"
threshold = 0.0
similarity_ratio_to_compare = SequenceMatcher(None, str1, "te gustan los animales?").ratio()
if similarity_ratio_to_compare > similarity_ratio and similarity_ratio_to_compare > threshold:
response_text = "puede que algunos me gusten pero no se mucho de eso"
similarity_ratio = similarity_ratio_to_compare
similarity_ratio_to_compare = SequenceMatcher(None, str1, "animales").ratio()
if similarity_ratio_to_compare > similarity_ratio and similarity_ratio_to_compare > threshold:
response_text = "puede que algunos me gusten pero no se mucho de eso"
similarity_ratio = similarity_ratio_to_compare
similarity_ratio_to_compare = SequenceMatcher(None, str1, "te gustan las plantas?").ratio()
if similarity_ratio_to_compare > similarity_ratio and similarity_ratio_to_compare > threshold:
response_text = "no se mucho sobre ellas"
similarity_ratio = similarity_ratio_to_compare
similarity_ratio_to_compare = SequenceMatcher(None, str1, "carnivoro").ratio()
if similarity_ratio_to_compare > similarity_ratio and similarity_ratio_to_compare > threshold:
response_text = "segun mis registros son aquellos que se alimentan de carne"
similarity_ratio = similarity_ratio_to_compare
return response_text
input_text = "te gusta saltar la soga bien piolon???"
text = check_function(input_text)
print(text)
In the end this file must be saved with the name check.py
, keep in mind that the number of lines inside the data_string
variable is not known (and in this case there are only 4 questions with 4 answers to prevent the question from being too long)