嗨,我正在尝试将 csv 文件转换为 yml 文件,但我能够获得预期的结构,这是我当前的代码,我是 yml 文件中的预期输出
import csv
import yaml
# Read the CSV file
with open('data.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
# Create the YAML data structure
nlu = []
current_intent = None
current_examples = []
for row in reader:
intent = row['intent']
examples = row['examples'].split('\n')
if intent != current_intent:
if current_intent:
nlu_item = {
'intent': current_intent,
'examples': current_examples
}
nlu.append(nlu_item)
current_intent = intent
current_examples = []
current_examples.extend(examples)
# Add the last intent definition
if current_intent:
nlu_item = {
'intent': current_intent,
'examples': current_examples
}
nlu.append(nlu_item)
# Dump the YAML data to a file
with open('data.yml', 'w') as yamlfile:
yaml.dump(nlu, yamlfile, indent=4)
这是 yml 的当前脚本输出,为什么意图是在示例完成之后出现的,请检查我想要的预期输出
examples:
- hello
- hello good afternoon
- hello good evening
- hello good morning
intent: greet
这是预期的输出
nlu:
- intent: greet
examples: |
- hey
- hello
- hi
- hello there
csv 文件 data.csv
intent,examples
greet,hello
greet,hello good afternoon
greet,hello good evening
greet,hello good morning
greet,hello good noon
我猜这就是您想要的,因为您的预期输出没有多大意义(文字样式
|
和缩进):
import csv
import yaml
# Read the CSV file
with open(r'data.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
# Create the YAML data structure
nlu = []
current_intent = None
current_examples = []
for row in reader:
intent = row['intent']
example = row['examples'] # Each row contains only one example, therefore no split('\n')
if current_intent is None: # Prevent that next if-block is executed for first row
current_intent = intent
if intent != current_intent:
if current_intent:
nlu_item = {
'intent': current_intent,
'examples': current_examples
}
nlu.append(nlu_item)
current_intent = intent
current_examples = []
current_examples.append(example) # Only one example: append instead of extend
# Add the last intent definition
if current_intent:
nlu_item = {
'intent': current_intent,
'examples': current_examples
}
nlu.append(nlu_item)
# Dump the YAML data to a file
with open('data.yml', 'w') as yamlfile:
# Dumping nlu only dumps its content
# yaml.dump(nlu, yamlfile, indent=4)
yaml.dump({"nlu": nlu}, yamlfile, indent=4, sort_keys=False)
给定 CSV 文件的输出:
nlu:
- intent: greet
examples:
- hello
- hello good afternoon
- hello good evening
- hello good morning
- hello good noon