Home > database >  How can I open csv files and read them and sort them based on the data inside it?
How can I open csv files and read them and sort them based on the data inside it?

Time:12-07

So I'm trying to find how to open csv files and sort all the details in it...

so an example of data contained in a CSV file is...

2,8dac2b,ewmzr,jewelry,phone0,9759243157894736,us,69.166.231.58,vasstdc27m7nks3
1,668d39,aeqok,furniture,phone1,9759243157894736,in,50.201.125.84,jmqlhflrzwuay9c
3,622r49,arqek,doctor,phone2,9759544365415694736,in,53.001.135.54,weqlhrerreuert6f

and so I'm trying to let a function sortCSV(File) to open the CSV file and sort it based on the very first number, which is 0, 1 ....

so the output should be

1,668d39,aeqok,furniture,phone1,9759243157894736,in,50.201.125.84,jmqlhflrzwuay9c
2,8dac2b,ewmzr,jewelry,phone0,9759243157894736,us,69.166.231.58,vasstdc27m7nks3
3,622r49,arqek,doctor,phone2,9759544365415694736,in,53.001.135.54,weqlhrerreuert6f

Here is my code so far, which clearly doesn't work....

import csv
def CSV2List(csvFilename: str):
    f = open(csvFilename)
    q = list(f)
    return q.sort()

What changes should I make to my code to make sure my code works??

CodePudding user response:

using pandas, set the first column as index and use sort_index to sort based on your index column:

import pandas as pd
file_path = '/data.csv'
df = pd.read_csv(file_path,header=None,index_col=0)
df = df.sort_index()
print(df)

CodePudding user response:

There's a number of ways you could handle this but one of the easiest would be to install Pandas (https://pandas.pydata.org/).

First off you most likely will need some titles of each column which should be on the first row of you CSV file. When you've added the column titles and installed pandas:

With pandas:

import pandas as pd 

dataframe = pd.read_csv(filepath, index=0)

This will set the first column as the index column and will be sorting on the index.


Another way I've had to handle CSV:s with difficult formatting (aka exporting form excel etc) is by reading the file as a regular file and then iterating the rows to handle them on my own.

final_data = []
with open (filepath, "r") as f:
    for row in f:
        # Split the row
        row_data = row.split(",")
        # Add to final data array
        final_data.append(row_data

# This sorts the final data based on first row
final_data.sort(key = lambda row: row[0])

# This returns a sorted list of rows of your CSV
return final_data

CodePudding user response:

try csv.reader(Filename)

import csv
def CSV2List(csvFilename: str):
    f = open(csvFilename)
    q = csv.reader(f)
    return q.sort(key=lambda x: x[0])

CodePudding user response:

Using the csv module:

import csv

def csv_to_list(filename: str):
   # use a context manager here
    with open(filename) as fh:
        reader = csv.reader(fh)
        # convert the first item to an int for sorting
        rows = [[int(num), *row] for num, *row in reader]

    # sort the rows based on that value
    return sorted(rows, key=lambda row: row[0])    

CodePudding user response:

This is not the best way to deal with CSV files but:

def CSV2List(csvFilename: str):
    f = open(csvFilename,'r')
    l = []
    for line in f:
        l.append(line.split(','))

    for item in l:
        item[0] = int(item[0])

    return sorted(l)


print(CSV2List('data.csv'))

However I would probably use pandas instead, it is a great module

  • Related