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

时间:2017-06-09 15:08:29

标签: python csv

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)

我一直收到此错误:

print(symbol, "{:,.lf}".format(float(tradedQty/1e6), "M INR", "{:,.lf}".format(pctChange*100), "%"))
TypeError: unsupported operand type(s) for /: 'str' and 'float

2 个答案:

答案 0 :(得分:1)

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

float(tradedQty)/1e6

我确定你打算写:

HazelcastInstance testInstance = Hazelcast.newHazelcastInstance(config);

答案 1 :(得分:0)

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

CSV文件只是字符串,因此您希望将数字数据转换为数字类型。试试tradedQty = float(row[9])