~extractpython.py

main
jackfood 1 year ago committed by GitHub
parent ce7d690945
commit c9e7574d02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,20 +1,59 @@
# Read the AI's response from 'aianswer.txt' import os
with open('aianswer.txt', 'r', encoding='utf-8') as file:
ai_response = file.read()
# Find and extract content between triple backticks (```python) and (```) # Try to read the AI's response from 'aianswer.txt'
start_marker = "```python" try:
end_marker = "```" with open('aianswer.txt', 'r', encoding='utf-8') as file:
start_index = ai_response.find(start_marker) ai_response = file.read()
end_index = ai_response.find(end_marker, start_index + len(start_marker)) except FileNotFoundError:
print("Error: 'aianswer.txt' not found")
ai_response = None
if start_index != -1 and end_index != -1: if ai_response is not None:
python_content = ai_response[start_index + len(start_marker):end_index] # Find and extract content between triple backticks (```python) and (```)
start_markers = ["```python", "```"]
end_marker = "```"
start_index = -1
for marker in start_markers:
start_index = ai_response.find(marker)
if start_index != -1:
start_marker = marker
break
# Save the extracted Python content to 'aipythonanswer.txt' as UTF-8 end_index = ai_response.find(end_marker, start_index + len(start_marker))
with open('aipythonanswer.txt', 'w', encoding='utf-8') as python_file:
python_file.write(python_content)
print("Python content extracted and saved to 'aipythonanswer.txt'") if start_index != -1 and end_index != -1:
python_content = ai_response[start_index + len(start_marker):end_index]
# Check if 'aipythonanswer.txt' exists and compare its content with python_content
aipythonanswer_path = 'aipythonanswer.txt'
utf8_path = 'utf8.txt'
try:
with open(aipythonanswer_path, 'r', encoding='utf-8') as aipython_file:
existing_content = aipython_file.read()
if existing_content != python_content:
# If python_content is different, delete 'aipythonanswer.txt' and create a new one
try:
os.remove(aipythonanswer_path)
with open(aipythonanswer_path, 'w', encoding='utf-8') as python_file:
python_file.write(python_content)
with open(utf8_path, 'w', encoding='utf-8') as utf8_file:
utf8_file.write(python_content)
print("Python content updated in 'aipythonanswer.txt' and saved to 'utf8.txt'")
except FileNotFoundError:
print("Error: 'aipythonanswer.txt' not found")
else:
print("Python content in 'aipythonanswer.txt' is the same as extracted content.")
except FileNotFoundError:
# If 'aipythonanswer.txt' doesn't exist, create it and save python_content to it
with open(aipythonanswer_path, 'w', encoding='utf-8') as python_file:
python_file.write(python_content)
print("Python content extracted and saved to 'aipythonanswer.txt'")
with open(utf8_path, 'w', encoding='utf-8') as utf8_file:
utf8_file.write(python_content)
print("Python content saved to 'utf8.txt'")
else:
print("No Python code found between triple backticks in 'aianswer.txt'")
else: else:
print("No Python code found between triple backticks in 'aianswer.txt'") print("No AI response found in 'aianswer.txt'")

Loading…
Cancel
Save