Comma/dot problem in exported .csv file

Issue #199 closed
Vladyslav Nenakhov created an issue

Hello,

I use a german windows 7 (64bit) version with my mss tool. If I export a flight track (see attachment), than i get a comma/dot problem in my ".csv" file.

Is it possible that mss dont use the decimal system of the computer, but instead use always british decimal system?

Is it possible to modify mss in such a way, that mss recognize which decimal system is used and it save the date in the correct way in ".csv" files?

Best regards Vlad

Comments (6)

  1. Reimar Bauer

    Please elaborate. I have dumped the first lines of your CSV file. How do you want to have it?

    OP-London
    Index;Location;Lat (+-90);Lon (+-180);Flightlevel;Pressure (hPa);Leg dist. (km);Cum. dist. (km);Comments
    0;EDMO;48.080;11.280;0.000;1013.250;0.000;0.000;
    1;;49.120;10.070;164.000;540.297;146.076;146.076;
    2;Ruhr area;51.510;7.180;164.000;540.297;336.170;482.247;
    3;Rotterdam;51.560;4.290;64.000;799.832;200.593;682.840;
    
  2. Joern Ungermann

    MSS uses the typical numerical formatting of python, which is probably American(?) with "." as a decimal separator. Using the locale settings would cause issues with exchanging files between computers. I'll have a look into that, if Python offers a way to properly handle this without having to deal with all kinds of operating systems.

    However, the export plugins are designed as plugins to facilitate the simple adoption of changes for the user. You should be able to simply copy the csv.py plugin to your .config/mss folder under a different name (where also the configuration json is placed) and make the necessary changes to use "," instead of ".".

    Something like this might work for you (untested):

    # -*- coding: utf-8 -*-
    """
    
        mslib.plugins.io.csv
        ~~~~~~~~~~~~~~~~~~~~
    
        plugin for csv format flight track export
    
        This file is part of mss.
    
        :copyright: Copyright 2008-2014 Deutsches Zentrum fuer Luft- und Raumfahrt e.V.
        :copyright: Copyright 2011-2014 Marc Rautenhaus (mr)
        :copyright: Copyright 2016-2017 by the mss team, see AUTHORS.
        :license: APACHE-2.0, see LICENSE for details.
    
        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at
    
           http://www.apache.org/licenses/LICENSE-2.0
    
        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.
    """
    
    from __future__ import absolute_import
    from __future__ import division
    
    from builtins import str
    
    import csv
    import mslib.msui.flighttrack as ft
    
    
    def save_to_csv(filename, name, waypoints):
        if not filename:
            raise ValueError("filename to save flight track cannot be None")
        with open(filename, "w") as out_file:
            csv_writer = csv.writer(out_file, dialect='excel', delimiter=";", lineterminator="\n")
            csv_writer.writerow([name.encode("ascii", "replace")])
            csv_writer.writerow(["Index", "Location", "Lat (+-90)", "Lon (+-180)", "Flightlevel", "Pressure (hPa)",
                                 "Leg dist. (km)", "Cum. dist. (km)", "Comments"])
            for i, wp in enumerate(waypoints):
                loc = str(wp.location).encode("ascii", "replace")
                lat = "{:.3f}".format(wp.lat).replace(".", ",")
                lon = "{:.3f}".format(wp.lon).replace(".", ",")
                lvl = "{:.3f}".format(wp.flightlevel).replace(".", ",")
                pre = "{:.3f}".format(wp.pressure / 100.).replace(".", ",")
                leg = "{:.3f}".format(wp.distance_to_prev).replace(".", ",")
                cum = "{:.3f}".format(wp.distance_total).replace(".", ",")
                com = str(wp.comments).encode("ascii", "replace")
                csv_writer.writerow([i, loc, lat, lon, lvl, pre, leg, cum, com])
    
    
    def load_from_csv(filename):
        waypoints = []
        with open(filename, "r") as in_file:
            lines = in_file.readlines()
        if len(lines) < 4:
            raise SyntaxError("CSV file requires at least 4 lines!")
        dialect = csv.Sniffer().sniff(lines[-1])
        csv_reader = csv.reader(lines, dialect=dialect)
        name = next(csv_reader)[0]
        next(csv_reader)  # header
        for row in csv_reader:
            wp = ft.Waypoint()
            wp.location = row[1]
            wp.lat = float(row[2].replace(",", "."))
            wp.lon = float(row[3].replace(",", "."))
            wp.flightlevel = float(row[4].replace(",", "."))
            wp.pressure = float(row[5].replace(",", ".")) * 100.
            wp.distance_to_prev = float(row[6].replace(",", "."))
            wp.distance_total = float(row[7].replace(",", "."))
            wp.comments = row[8]
            waypoints.append(wp)
        return name, waypoints
    
  3. Vladyslav Nenakhov reporter

    Hello,

    thank you very much for your fast reply!

    @Reimber Bauer: i will try first the solution way of Joern Ungermann. It is right now the fastest way to get right values in *.csv without bothering you.

    @ Joern Ungermann: thank you very much for you code!

  4. Reimar Bauer

    This is not a bug, because mss uses numerical formatting of python, which is a design decission to have exchangable files.

    Anyway this is a plugin one can modify for his needs.

  5. Joern Ungermann

    I had a look into the python locale module. It offers, in principle, the desired functionality, but changing the locale might have side-effects to other libraries. Further, the code pages are seemingly not identical between Windows and Unix. Personally, I would say that the plugin-modifying solution is simplest here.

    If Vlad has his problem solved, I would propose to close this as resolved.

  6. Log in to comment