'How would i get specific data value from a csv file without using modules?
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
Solution 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]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
