如何使用python提取csv文件的最大值和最小值

时间:2018-02-21 12:23:12

标签: python list csv readline

我有一个从包含3列

的csv文件中读取的python脚本
  • 日期
  • minTemprature
  • maxTemperature

我需要提取

  1. 最小值存在于minTemprature列
  2. maxTemperature列中存在的最大值
  3. 我的脚本中的错误在哪里?

    的代码:

    function GetScores(url, onCountTotal) {
        var FBUrl, TWUrl, LNUrl, GLUrl, PIUrl;
        var url1 = "";
        url1 = encodeURIComponent(url1 || url);
        //Fetch counters from PInterest
        var pi_like_count = 0;
        PIUrl = "https://api.pinterest.com/v1/urls/count.json?url=" + url1 + "&format=jsonp" + '&callback=?';
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: PIUrl,
            success: function (data) {
                pi_like_count = data.count;
                var fb_share_count = 0;
                FBUrl = "https://graph.facebook.com/?id=" + url1 + "&format=json";
                $.ajax({
                    type: 'GET',
                    dataType: 'json',
                    url: FBUrl,
                    success: function (data) {
                        fb_share_count = data.share.share_count;
                        var totalshare = parseInt(fb_share_count) + parseInt(pi_like_count);
                        onCountTotal(totalshare);
                        //alert(fb_share_count + ' facebook');
                    },
                    error: function (data) {
                        onCountTotal(-1);
                    },
                    async: true
                });
            },
            error: function (req, status, error) {
                onCountTotal(-1);
            },
            async: true
        });
    }
    
    //EXAMPLE HERE CALL FUNCTION WITH CALLBACK
    GetScores("http://www.google.com", function (count) {
        alert("Count = " + count);
    });
    

2 个答案:

答案 0 :(得分:1)

如果您只需要最小值和最大值,您可以执行以下操作:

编辑:根据评论更改了代码

import csv

with open("C:/Users/test/Documents/R_projects/homework/rdu-weather-history.csv") as myfile:
    myreader=csv.reader(myfile,delimiter=';')
    content = list(myreader)
    minTemp = min([int(elem[1]) for elem in content])
    maxTemp = max([int(elem[2]) for elem in content])

print("the min value is : ", minTemp)
print("the max value is : ", maxTemp) 

答案 1 :(得分:0)

使用Numpy

import numpy as np
data = np.loadtxt("C:/Users/test/Documents/R_projects/homework/rdu-weather-history.csv", delimiter=',', usecols=(1,2)

min_min_T = np.min(data[:, 0])
max_max_T = np.max(data[:, 1])