[Python] How to disable single quote which is printed by default in a file while using Python...

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I am facing issues in YAML module of Python. I am trying to read an Excel sheet and trying to print the data into a YAML file. I have the below code:

    import pandas as pd
    import yaml
    import os

    # Define file paths
    directory = "C:\\var\\opt"
    file_name = "contextTag.xlsx"
    file_path = os.path.join(directory, file_name)
    sheet_name = "cf_set_attributes"
    columns = ["attribute_value_list"]
    yaml_file_path = os.path.join(directory, "output.yaml") # Single YAML file

    # Read the Excel sheet into a DataFrame
    df = pd.read_excel(file_path, sheet_name=sheet_name, usecols=columns)

    yaml_data = {
    "apiVersion": "cct/v1",
    "kind": "filter",
    "metadata": {
    "name": "test"
    },
    "spec": {
    "parameters": {
    "attributes": []
    },
    }
    }

    for _, row in df.iterrows():
    row_data = row.to_dict()
    attribute_entries = str(row_data["attribute_value_list"]).split(",")
    attributes_list = []
    for entry in attribute_entries:
    attr_parts = entry.split(":") # Split each entry by ':'
    if len(attr_parts) == 2: # Ensure valid format
    attributes_list.append(
    {"attribute": attr_parts[0].strip(), "value": attr_parts[1].strip()})

    yaml_data["spec"]["parameters"]["attributes"].append(attributes_list)

    with open(yaml_file_path, "w") as yaml_file:
    yaml.dump(yaml_data, yaml_file, default_flow_style=False,allow_unicode=True,sort_keys=False)
    print(f"YAML file created successfully at {yaml_file_path}.")


    The input shared is as shown below attribute_value_list has below values

    model: merc,
    ver: cxv1


    Incorrect output

    apiVersion: cct/v1
    kind: filter
    metadata:
    name: test
    spec:
    parameters:
    attributes:
    - - attribute: '"vendor_name"'
    value: '"''Ericsson''"'
    - attribute: '"technology"'
    value: '"''2G''"'


    Required output with the correct quotes:

    apiVersion: cct/v1
    kind: filter
    metadata:
    name: test
    spec:
    parameters:
    attributes:
    - attribute: "vendor_name"
    value: "'Ericsson'"
    attribute: "technology"
    value: "'2G'"


    How can I get the correct quotes which is needed?

    Continue reading...

Compartilhe esta Página