Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不允许使用字符串?如何将范围的最大限制定义为csv列中的单元格值?_Python_Arrays_Csv_Enumeration - Fatal编程技术网

Python 不允许使用字符串?如何将范围的最大限制定义为csv列中的单元格值?

Python 不允许使用字符串?如何将范围的最大限制定义为csv列中的单元格值?,python,arrays,csv,enumeration,Python,Arrays,Csv,Enumeration,希望不正确的代码仍然能传达我正在尝试做的事情。我使用enumerate(and range)得到一个字符串错误,b/c“count”被解释为字符串,而我希望它被解释为整数值。我想创建从0到计数值的整数值数组。如果有帮助的话,我的目的是在只给出每个值的频率时创建一个值列表。谢谢 import csv, sys outputCSV = open(r"C:\Users\Out.csv") inputCSV = open(r"C:\Users\Slope.csv") reader = csv.rea

希望不正确的代码仍然能传达我正在尝试做的事情。我使用enumerate(and range)得到一个字符串错误,b/c“count”被解释为字符串,而我希望它被解释为整数值。我想创建从0到计数值的整数值数组。如果有帮助的话,我的目的是在只给出每个值的频率时创建一个值列表。谢谢

import csv, sys

outputCSV = open(r"C:\Users\Out.csv")
inputCSV = open(r"C:\Users\Slope.csv")

reader = csv.reader(inputCSV, delimiter = ',')
writer = csv.writer(outputCSV, delimiter = ',')

for row in reader:
    count = row[1]
    countArray = enumerate(0, count) #make list of consecutive integers from 0 to value of count

    while i <= max(countArray):
        print row[0]                #print row value as many times as there are integers in the range 0 through the max of countArray. The printed row value should have same index as the current count index.
导入csv,系统
outputCSV=open(r“C:\Users\Out.csv”)
inputCSV=打开(r“C:\Users\Slope.csv”)
reader=csv.reader(inputCSV,分隔符=',')
writer=csv.writer(outputCSV,分隔符=',')
对于读取器中的行:
计数=行[1]
countArray=枚举(0,count)#列出从0到count值的连续整数

虽然我这里有几个问题。我不知道你想做什么

1) 要消除错误,只需将计数转换为整数:

count = int(row[1])
2) 要创建介于0和计数之间的列表,请执行以下操作:

countArray = range(count+1)
3) 您有一个无限循环:

# i is not initialized => another error
while i <= max(countArray): # max(countArray) = count => why create the array in the first place?
    print row[0]            # i is not altered so i <= max(countArray) is always True => infinite loop:

据我所知,CSV文件中的每一行都有一个数字和一个计数;您需要为每一行创建一个列表,其中包含重复计数次数

在使用它时,不会出现字符串错误-使用enumerate是错误的,因为它需要一个iterable作为第一个参数。但在这种情况下,无论如何都不需要枚举

但一般来说,从csv读取的内容将是一个字符串,因此需要将其转换为整数。你可以这样做:

for row in reader:
    count = int(row[1])
    num = int(row[0])
    for i in range(count):
        print(num)
或者,您可以使用理解功能,一次获取所有列表:

nums = [[int(row[0])] * int(row[1]) for row in reader]

@Julien Spronck你们知道为什么我在使用int(第[1]行)时会得到以下结果吗---以10为基数的int()的文本无效:“COUNT”谢谢提示*编辑,哦,别担心,那是标题!我想那一定是头球吧?第一行可能有(COUNT,NUM)或其他内容。您应该跳过该行-请参见:字符串“COUNT”不能转换为整数。我猜这是你文件的第一行,需要跳过。再次感谢各位,我现在想知道我在输出csv中将num返回写入列时的错误是什么。我尝试将此作为第二个for循环的最后一行------writer.writerow(num)您不能“writerow”一个数字-它需要一个iterable。参考
nums = [[int(row[0])] * int(row[1]) for row in reader]