TypeError:强制使用Unicode:需要字符串或缓冲区,在运行python文件时找到类型
我有一个python脚本,它从.csv文件中读取数据,并使用它对数据进行数学计算。当我运行它时,会出现以下错误:TypeError:强制使用Unicode:需要字符串或缓冲区,在运行python文件时找到类型,python,csv,unicode,typeerror,argparse,Python,Csv,Unicode,Typeerror,Argparse,我有一个python脚本,它从.csv文件中读取数据,并使用它对数据进行数学计算。当我运行它时,会出现以下错误: Traceback (most recent call last): File "HW1_PythonTemplate.py", line 120, in <module> print ','.join(map(str,calculate(args.data, args.i))) File "HW1_PythonTemplate.py", line 56,
Traceback (most recent call last):
File "HW1_PythonTemplate.py", line 120, in <module>
print ','.join(map(str,calculate(args.data, args.i)))
File "HW1_PythonTemplate.py", line 56, in calculate
with open(file, 'r') as csvfile:
TypeError: coercing to Unicode: need string or buffer, type found
在calculate函数中,过去的一切都是任意数学
我的主要想法是:
import argparse
import csv
import sys
def calculate( dataFile, ithAttr):
numObj, minValue, maxValue, mean, stdev, Q1, median, Q3, IQR = [0,"inf","-inf",0,0,0,0,0,0]
rows = []
with open(file, 'r') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
rows.append(row)
columniStr = [row[ithAttr-1] for row in rows]
columniFloat = []
for value in columniStr:
try:
columniFloat.append(float(value))
except ValueError:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='calc')
parser.add_argument('--i', type=int,
help="ith attribute of the dataset (2 <= i <= 29)",
default=5,
choices=range(2,30),
required=True)
parser.add_argument("--data", type=str,
help="Location of the dataset file",
default="energydata_complete.csv",
required=True)
args = parser.parse_args()
print ','.join(map(str,calculate(args.data, args.i)))
如果名称=“\uuuuu main\uuuuuuuu”:
parser=argparse.ArgumentParser(description='calc')
add_参数('--i',type=int,
help=“数据集的第i个属性(2
您拼错了数据文件
file
是文件对象的内置Python数据类型,因此您无意中试图打开一个类型
您拼错了数据文件
file
是文件对象的内置Python数据类型,因此您无意中试图打开一个类型。这是正确的,谢谢!我很困惑,但现在它工作得很好这是正确的,谢谢!我很困惑,但现在它工作得很好
with open(file