是1e6导致我出现类型错误吗?

2024-10-01 15:47:18 发布

您现在位置:Python中文网/ 问答频道 /正文

import csv

oneFileName = listOfFiles[0]

lineNum = 0

listOfLists = []

with open(oneFileName,"rt") as csvfile:
    lineReader = csv.reader(csvfile,delimiter=",",quotechar="\"")
    for row in lineReader:
        lineNum = (lineNum + 1)
        if lineNum == 1:
            print("Skipping the header row")
            continue
        symbol = row[0]
        close = row[5]
        prevClose = row[7]
        tradedQty = row[9]
        pctChange = float(float(close)/float(prevClose) - 1)
        oneResultRow = [symbol, pctChange,float(tradedQty)]
        listOfLists.append(oneResultRow)
        print(symbol, "{:,.lf}".format(float(tradedQty/1e6), "M INR", "{:,.lf}".format(pctChange*100), "%"))
    print("Done iterating over the file contents - the file is closed now!")
    print("We have stock info for " + str(len(listOfLists)))

listOfListsSortedByQty = sorted(listOfLists, key=lambda x:x[2], reverse=True)

listOfListsSortedByQty = sorted(listOfLists, key=lambda x: x[1], reverse=True)

我一直收到这个错误:

^{pr2}$

Tags: csvthecsvfileforclosefloatsymbolrow
2条回答

tradedQty是一个字符串。1e6是数字,不是问题所在。在

CSV文件只是字符串,所以您需要将数字数据转换为数字类型。尝试tradedQty = float(row[9])

不,问题是你把括号放错地方了。在

    float(tradedQty/1e6)
//        ^^^^^^^^^ ^^^
//        string  float
//  ^^^^^
// too late

我肯定你是想写:

^{pr2}$

相关问题 更多 >

    热门问题