-1

So while reading a CSV-file into python, some of the variables have the following structure: '"variable"' I stored them in listed tuples. Now, some of these variables have to be compared to each other as they are numeric. But I can't seem to find a way to compare them to each other. For example:

counter = 0
if '"120000"' < '"130000"':
        counter += 1

However, the counter remains at 0. Any advice on how to work with these types of datastructures? I tryed converting them to integers but this gives my a ValueError.

The original file has the following layout:

Date,"string","string","string","string","integer"

I read the file as follows:

with open(dataset, mode="r") as flight_information:
    flight_information_header = flight_information.readline()
    flight_information = flight_information.read()
    flight_information = flight_information.splitlines()
    flight_information_list = []
    for lines in flight_information:
        lines = lines.split(",")
        flight_information_tuple = tuple(lines)
        flight_information_list.append(flight_information_tuple)
Fred
  • 3
  • 3
  • Could you give more context - how are you reading from the CSV, what does it look like, how are those numerical values getting quoted to start with? You could strip the quotes before conversion to integer, but it might be better fixed upstream. – jonrsharpe Nov 29 '20 at 11:43
  • Posting my code as an answer – Fred Nov 29 '20 at 13:04
  • It's *not* an answer, edit it into the question. Why is the integer quoted to start with? – jonrsharpe Nov 29 '20 at 13:20
  • Build-up of the CSV file, can't change anything about it. – Fred Nov 29 '20 at 13:24
  • So did you try stripping the quotes back out? There are loads of ways to do it, see e.g. https://stackoverflow.com/questions/40950791/remove-quotes-from-string-in-python. – jonrsharpe Nov 29 '20 at 13:24
  • So I tryed the following 2: .replace('"', '') and .strip('"'). i did this with using 2 forloops to go over all the variables within each tuple for each tuple in the list. However it doesn't seem to work. – Fred Nov 29 '20 at 13:37
  • Then give a [mre] to explain what *"doesn't seem to work"* means. – jonrsharpe Nov 29 '20 at 13:52

1 Answers1

-1

For people in the future, the following solved my problem: Since the tuples are immutable I now removed the "" around my numerical values while loading the csv file:

Example:

with open(dataset, mode="r") as flight_information:
    flight_information_header = flight_information.readline()
    flight_information = flight_information.read()
    flight_information = flight_information.splitlines()
    flight_information_list = []
    for lines in flight_information:
        lines = lines.replace('"', '').split(",")
        flight_information_tuple = tuple(lines)
        flight_information_list.append(flight_information_tuple)

Note this line in particular:

lines = lines.replace('"', '').split(",")
Fred
  • 3
  • 3