Reputation: 300
I have the following csv file which contains these rows:
;name;;;surname;;age;salary;
0;john;;;snow;;38;1000;
1;nick;;;kalathes;;22;500;
0;roger;;;smith;;21;400;
1;mona;;;liza;;18;1000;
1;bill;;;alios;;48;2000;
I read csv file using csv.reader
:
import csv,sys
filename='test1.csv'
with open(filename, mode='r', newline='', encoding='utf-8') as f:
reader=csv.reader(f)
try:
for row in reader:
print(row)
Output:
[';name;;;surname;;age;salary;']
['0;john;;;snow;;38;1000;']
['1;nick;;;kalathes;;22;500;']
['0;roger;;;smith;;21;400;']
['1;mona;;;liza;;18;1000;']
['1;bill;;;alios;;48;2000;']
My goal is to find the maximum age if a row starts with "1"
.
I suppose that as a first step I have to find out the position of string 'age'
at the first row. (in this example age is the seventh element (separated by ;
).
I work on that by trying something like:
for row in reader:
print(row)
print(type(row))
indexes = [i for i,x in enumerate(row) if x == "age"]
print(indexes)
But had no success so far. I think that when I will be able to know the position of that I will have to check if any rows start with "1"
. after that I have to find the max and the min from these rows. I'm able to do it in java, javscript, c, etc. but I'm a beginner in python. A pseudo-code I have to implement may be something like:
for(i=0; i<list.length; i++ {
if (list.includes("age") {
position=stringAt(age)
break;
}
for(i=0; i<list.length; i++ {
if (list[0]==1) {
tmp.push(list[position]
}
print(max.tmp)
Upvotes: 0
Views: 1329
Reputation: 1675
I'd use DictReader
and the min
and max
built in functions:
import csv,sys
filename='test1.csv'
with open(filename, mode='r', newline='', encoding='utf-8') as f:
reader=csv.DictReader(f, delimiter=";")
print(min(int(row['age']) for row in reader))
with open(filename, mode='r', newline='', encoding='utf-8') as f:
reader=csv.DictReader(f, delimiter=";")
print(max(int(row['age']) for row in reader))
You may need to use try
and except
if the data is not guaranteed.
Upvotes: 2
Reputation: 123521
Since your CSV file is delimited by semicolons, you need to specify that when creating the csv.reader
. The rest of the processing is relatively straight-forward:
import csv, sys
DELIMITER = ';'
FILENAME = 'minmax_test.csv'
with open(FILENAME, mode='r', newline='', encoding='utf-8') as f:
reader = csv.reader(f, delimiter=DELIMITER)
# Find index of "age" field.
header = next(reader)
for i, field in enumerate(header):
if field == 'age':
age_index = i
break
else:
raise RuntimeError('No field named "age" found in csv file')
# Find min and max ages of rows that start with "1".
min_age, max_age = sys.maxsize, -sys.maxsize-1
min_age_row, max_age_row = None, None
min_age_name, max_age_name = '', ''
for i, row in enumerate(reader):
print('row[{}]: {}'.format(i, row))
if row[0] == '1':
age = int(row[age_index])
if age < min_age:
min_age = age
min_age_row = i
min_age_name = row[1]
if age > max_age:
max_age = age
max_age_row = i
max_age_name = row[1]
print('min - name: {!r}, age: {} in row {}'.format(min_age_name, min_age, min_age_row))
print('max - name: {!r}, age: {} in row {}'.format(max_age_name, max_age, max_age_row))
Output:
row[0]: ['0', 'john', '', '', 'snow', '', '38', '1000', '']
row[1]: ['1', 'nick', '', '', 'kalathes', '', '22', '500', '']
row[2]: ['0', 'roger', '', '', 'smith', '', '21', '400', '']
row[3]: ['1', 'mona', '', '', 'liza', '', '18', '1000', '']
row[4]: ['1', 'bill', '', '', 'alios', '', '48', '2000', '']
min - name: 'mona', age: 18 in row 3
max - name: 'bill', age: 48 in row 4
Upvotes: 2
Reputation: 10820
Specify semicolon as your delimiter, then it's fairly straightforward:
reader = csv.reader(f, delimiter=';')
ages = [row[6] for row in reader if row[0] == '1']
max_age = sorted(ages)[-1]
Upvotes: 2
Reputation: 93
You should always read the docs (CSV File Reading and Writing).
Your CSV is not a "standard" CSV file so you need to specify your delimiter (';')
reader=csv.reader(f, delimiter=';')
Now your output will be as follow:
['0','john','','','snow','','38','1000','']
['1','nick','','','kalathes','','22','500','']
Upvotes: 1