0

I'm working on a project in which I'm not allowed to import any modules. I need to do some calculations with specific data values from a CSV file, but in order to do that i would need to find them first. I don't know how to get my program to find the values of the 3rd,4th and 5th column in a specific row where column[1] is a specific number. Since the numbers in a given CSV file for column[1] would not necessarily be in numerical order, i need to arrange the 3rd,4th and 5th column in numerical order of the column[1] ascending from 1. Any help would be much appreciated.

J1283,1,-0.837028634,0.724271463,0.718634041
J1283,2,-0.836510147,-0.722637551,0.718199937
J1283,6,-0.833234927,-0.722650022,0.71765857
J1283,4,-0.829230547,-0.723707934,-0.717132914
J1283,3,-0.81601564,-0.722668297,0.717088805
J1283,5,-0.808930225,-0.722808277,0.718237702

                                    

1 Answer 1

1

CSV is nothing but a file where values are separated by commas (Comma-Separated Values, that's the name).

You just need to read the file and parse it line by line :

def read_csv(filename: str) -> list[list[str]]:
    values = []
    with open(p, 'r') as f:
        for l in f.readlines():
            values.append(l.split(','))
    return values

This needs absolutely no import statement. You can modulate this to go in accordance to what you want to do, like converting the 2nd column to int and the 3rd to last column to float :

def read_csv(filename: str) -> list[list[str]]:
    values = []
    with open(filename, 'r') as f:
        for l in f.readlines():
            values.append(l.strip('\n').split(','))
    for row in values:
        row[1] = int(row[1])
        row[2:5] = [float(i) for i in row[2:5]]
    return values

The, you can use this result to sort your rows with the sort method :

if __name__ == '__main__':
    values = read_csv('my_file.csv')
    values.sort(key=lambda row: row[1])

Result is the following :

['J1283', 1, -0.837028634, 0.724271463, 0.718634041]
['J1283', 2, -0.836510147, -0.722637551, 0.718199937]
['J1283', 3, -0.81601564, -0.722668297, 0.717088805]
['J1283', 4, -0.829230547, -0.723707934, -0.717132914]
['J1283', 5, -0.808930225, -0.722808277, 0.718237702]
['J1283', 6, -0.833234927, -0.722650022, 0.71765857]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, @imperosol. I sort of understand it now. Also, in the csv file I was given, column[0] isn't always the same. For example, a chunk of the file is as follows: J7033,7,0.878833765,0.72272582,0.725302538 J7033,8,-0.867293118,-0.723656965,-0.724962529 J1283,1,-0.837028634,0.724271463,0.718634041 J1283,2,-0.836510147,-0.722637551,0.718199937 How would i sort column[1] only in which column[0] is the same. For example, if column[0] is x in 5 rows and column[0] is y in another 5 rows, how would I sort the 5 column[1] values for x separately from the 5 column[1] values for y?
you can extract the values you want to sort with their indexes. You can then sort the resulting list and finally replace it with the indexes you kept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.