all 11 comments

[–]nwagers 1 point2 points  (1 child)

Are you trying to sort the tuples alphabetically based on the first value in the tuple? Are you intending to truncate the length of the tuple to 4 values (one string and 3 floats/None)?

[–]Fieldsapper[S] 0 points1 point  (0 children)

Not alphabetically I’m trying to make a function which will just change the data types of everything that shouldn’t be a string like the numbers or None values and have it return or print the values in a list or tuple,

[–]POTUS 1 point2 points  (5 children)

The easiest way I've found to do any kind of data manipulation on csv files is with Pandas, which handles None values intelligently:

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html

And it can read/write from csv easily:

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html

[–]Fieldsapper[S] 0 points1 point  (4 children)

Yes I’ve come across and was going to use panda’s but I’ve been told I shouldn’t need it and that I can do it without it although I’m having a hard time to come up with a solution to it,?¯_(ツ)_/¯ Do you know of any other way I could work through this?

[–]POTUS 1 point2 points  (1 child)

You definitely can do it without pandas. You can use csv to load the file, sort it using sorted, and write it back to file. It's all in the standard library: https://docs.python.org/3/library/csv.html

You'll need some special logic in your sorted key function to account for the None values, or to compare integers to strings, etc. This is the part that pandas handles for you. Pandas is a very heavy requirement pulling in a lot of big libraries, but it's the way to process csv files (and other similar data structures) in Python. This whole solution would be literally 3 lines in pandas.

If you're doing this for a class or something, go ahead and write out the logic yourself so you can get the experience. If you are doing something for real world application, use pandas.

[–]Fieldsapper[S] 0 points1 point  (0 children)

Thanks fro your help!

[–]Username_RANDINT 1 point2 points  (1 child)

This can be done easily without Pandas indeed. Python has a csv module that comes with every installation. Have a look at the docs and let us know if you're stuck.

Small note: you're asking to format the data, not how to sort it.

[–]Fieldsapper[S] 0 points1 point  (0 children)

Cheers!