You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used ChatGPT to generate a piece of code, he can read wildcard file to generate yaml, can complete most of the work, but need to manually modify,His main problems were unexpected indentation and grammar problems, please help me finish it, thank you!
import os
import yaml
def generate_nested_yaml(folder_path):
data = {}
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".txt"):
file_path = os.path.join(root, file)
with open(file_path, "r", encoding="utf-8") as f:
content = [line.strip() for line in f.readlines()]
nested_keys = os.path.relpath(file_path, folder_path).rsplit(".", 1)[0].split(os.sep)
nested_dict =data
for key in nested_keys:
nested_dict = nested_dict.setdefault(key, {})
#print (key)
nested_dict[nested_keys[-1]] = content
with open(os.path.join(folder_path, "nested.yaml"), "w", encoding="utf-8") as f:
yaml.dump(data, f, allow_unicode=True, default_flow_style=False)
# Provide the target folder path
target_folder = r"D:\Desktop\wildcards"
# Call the function to generate the nested YAML file
generate_nested_yaml(target_folder)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I used ChatGPT to generate a piece of code, he can read wildcard file to generate yaml, can complete most of the work, but need to manually modify,His main problems were unexpected indentation and grammar problems, please help me finish it, thank you!
Beta Was this translation helpful? Give feedback.
All reactions