从CSV文件中获取重复数据

时间:2018-10-01 16:42:52

标签: python json csv

def general_number(request):
    csvfilename = 'algo/generalnumber.csv'
    csvfile = open(csvfilename, 'r')
    reader = csv.DictReader(csvfile)
    fieldnames = ("Ticker","Company","Industry","PreviousClose","Open","Bid","Ask","DaysRange","ftWeekRange","Volume","AvgVolume","MarketCap","Beta","PERatioTTM","EPSTTM","EarningsDate","ForwardDividendYield","ExDividendDate","OneyTargetEst","ticker_company")
    output = []
    for each in reader:
      row = {}
      for field in fieldnames:
        row[field] = each[field]
        output.append(row)
    return JsonResponse(output[20:30],safe=False)

在这里,我正在从csv中获取json中的重复数据。 每个数据显示10次。怎么了?

1 个答案:

答案 0 :(得分:1)

这只是一个缩进问题。修复它,请立即尝试!

def general_number(request):
    csvfilename = 'algo/generalnumber.csv'
    csvfile = open(csvfilename, 'r')
    reader = csv.DictReader(csvfile)
    fieldnames = ("Ticker","Company","Industry","PreviousClose","Open","Bid","Ask","DaysRange","ftWeekRange","Volume","AvgVolume","MarketCap","Beta","PERatioTTM","EPSTTM","EarningsDate","ForwardDividendYield","ExDividendDate","OneyTargetEst","ticker_company")
    output = []
    for each in reader:
      row = {}
      for field in fieldnames:
        row[field] = each[field]
      output.append(row)
    return JsonResponse(output[20:30],safe=False)