-1

Hopefully the incorrect code still conveys what I'm trying to do. I get a string error using enumerate (and range) b/c 'count' is interpreted as a string when I instead want it to be interpreted as an integer value. I want to create arrays with integer values from 0 to the count value. If it helps, my purpose is to create a single list of values when only the frequency of each value is given. Thanks!

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.
AF2k15
  • 250
  • 5
  • 19

2 Answers2

0

There are several problems here. I am not sure what you are trying to do.

1) To get rid of the error, just transform your count into an integer:

count = int(row[1])

2) To create a list that ranges between 0 and count:

countArray = range(count+1)

3) you have an infinite loop:

# 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:

Maybe what you want is something like:

for i in countArray:
    print row[0] # That loop works but i don't know if it does what you need 
Julien Spronck
  • 15,069
  • 4
  • 47
  • 55
0

From what I understood, each row in your CSV file has a number and a count; and you want to create, for each row, a list with the number repeated count times.

As you have used it, you will not get a string error - you are using enumerate wrong since it expects an iterable as the first argument. But in this case, enumerate is not needed anyways.

In general though, what you read from the csv will be a string, so you need to convert it into an integer. You could do something like this:

for row in reader:
    count = int(row[1])
    num = int(row[0])
    for i in range(count):
        print(num)

Or you could use comprehension and get all the lists at once:

nums = [[int(row[0])] * int(row[1]) for row in reader]
Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52
  • @Julien Spronck Either of you know why I would be getting the following when I use int(row[1])? ---invalid literal for int() with base 10: 'COUNT' Thanks for the tips. *edit, oh nevermind, that's the header! – AF2k15 Apr 17 '15 at 04:55
  • That must be the header I suppose? The first row probably has (COUNT, NUM) or something. You should skip that line - see this: http://stackoverflow.com/questions/14257373/skip-the-headers-when-editing-a-csv-file-using-python – Jayanth Koushik Apr 17 '15 at 04:58
  • the string 'COUNT' cannot be converted into an integer. I'm guessing that's the first line of your file, that needs to be skipped. – Julien Spronck Apr 17 '15 at 04:59
  • Thanks again guys, I'm now wondering what my error is with writing the num returns to a column in an output csv. I'm trying this as the last line in the second for loop ------ writer.writerow(num) – AF2k15 Apr 17 '15 at 05:10
  • you can't "writerow" a number - it expects an iterable. Refer https://docs.python.org/3/library/csv.html#csv.csvwriter.writerow – Jayanth Koushik Apr 17 '15 at 05:13