我有以下包含这些行的csv文件
;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;
我使用csv.reader读取csv文件
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;']
我的目标是找到以“ 1”开头的行的最大年龄。 我想作为第一步,我必须找出字符串“ age”在第一行的位置。 (在此示例中,年龄是第七个元素(以;分隔)
我通过尝试
来解决这个问题 print(row)
print(type(row))
indexes = [i for i,x in enumerate(row) if x == "age"]
print(indexes)
但到目前为止没有成功。我认为,当我能够知道其位置时,我将必须检查是否有任何以“ 1”开头的行。之后,我必须从这些行中找到最大值和最小值。我能够在java,javscript,c等中执行此操作。但是我是python的初学者。我必须实现的伪代码可能类似于:
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)
答案 0 :(得分:1)
将分号指定为分隔符,这非常简单:
reader = csv.reader(f, delimiter=';')
ages = [row[6] for row in reader if row[0] == '1']
max_age = sorted(ages)[-1]
答案 1 :(得分:1)
我将使用DictReader
以及内置的函数min
和max
:
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))
print(max(int(row['age']) for row in reader))
如果不能保证数据,则可能需要使用try
和except
。
答案 2 :(得分:1)
由于您的CSV文件用分号分隔,因此在创建csv.reader
时需要指定该值。其余处理相对简单:
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))
输出:
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
答案 3 :(得分:0)
您应该始终阅读文档(CSV File Reading and Writing)。
您的CSV文件不是“标准” CSV文件,因此您需要指定定界符(';')
reader=csv.reader(f, delimiter=';')
现在您的输出将如下:
['0','john','','','雪','','38','1000','']
['1','nick','','','kalathes','','22','500','']