Writing to file gives me other result when I use dictionary values in Python

29 Views Asked by At

I'm making a .csv to .xml converter as a school Project in Python. However, I don't seem to get the formatting right, even though the two values should be identical.

This is the code I use to write the content of the .csv to a .xml:

def _convert_to_xml(self, in_file, out_file, separator, xml_dict):
        """Converts a .csv to .xml."""
        if separator is None:
            separator = ","

        if xml_dict is None:
            xml_dict = {
                "indent": "  ",
                "newline": "\n",
                "row_tag": "row",
                "col_tag":  "col",
                "table_tag": "table"
            }

        rows = []

        with open(in_file, 'r') as input_file:
            raw_text = input_file.read()
            raw_rows = raw_text.splitlines()
            for raw_row in raw_rows:
                print(raw_row)
                rows.append(raw_row.split(separator))
                print(rows)

        with open(out_file, 'w') as output_file:
            output_file.write(f"<{xml_dict['table_tag']}>")
            output_file.write("\n")
            for row in rows:
                output_file.write(f"{xml_dict['indent']}<{xml_dict['row_tag']}>")
                output_file.write("\n")
                for col in row:
                    output_file.write(f"{2*xml_dict['indent']}<{xml_dict['col_tag']}>")
                    output_file.write(col)
                    output_file.write(f"</{xml_dict['col_tag']}>")
                    output_file.write("\n")
                output_file.write(f"{xml_dict['indent']}</{xml_dict['row_tag']}>")
                output_file.write("\n")
            output_file.write(f"</{xml_dict['table_tag']}>")

When I run the code, I get the following result I'm satisfied with:

<table>
  <row>
    <col>Robert</col>
    <col>Richman</col>
    <col>Felix</col>
    <col>Alexander</col>
  </row>
  <row>
    <col>Daniela</col>
    <col>Daniel</col>
    <col>Danuta</col>
  </row>
  <row>
    <col>Hier</col>
    <col>Nurzwei</col>
  </row>
  <row>
    <col>eins</col>
  </row>
</table>

The converter works via CLI, that means the user can e.g. input a optional argument for the values in xml_dict. When no argument for xml_dict is given, the program instead uses my default xml_dict values, as you can see in the code. In an effort to make the program more customizable, I decided to write output_file.write(xml_dict['newline']) instead of output_file.write("\n") should the user ever have a preference to use his own newline value.

However, after I replace all "\n" with (xml_dict['newline'], I get this result:

<table>

  <row>

    <col>Robert</col>

    <col>Richman</col>

    <col>Felix</col>

    <col>Alexander</col>

  </row>

  <row>

    <col>Daniela</col>

    <col>Daniel</col>

    <col>Danuta</col>

  </row>

  <row>

    <col>Hier</col>

    <col>Nurzwei</col>

  </row>

  <row>

    <col>eins</col>

  </row>

</table>

As you can see, I now have one empty line after every element instead of just a new line! How can that be when "\n" and (xml_dict['newline'] are the same, and how do I fix this?

0

There are 0 best solutions below