0

I basically want to read an element from a list and use it in the range function in a for loop as shown below

import csv
with open('input.csv', 'r') as f:
    reader = csv.reader(f)
    data_list = list(reader)

for i in range (0,210):
    chain_no = data_list[i][0]
    no_of_links = data_list[i][1]
    for j in range (2, 2*(int(data_list[i][3]))):
        link1 = []
        link1.append(data_list[i][j])

I get the error "ValueError: invalid literal for int() with base 10: ''"

Can some one please help me out

Sankarshana
  • 53
  • 1
  • 5
  • 1
    Possible duplicate of [ValueError: invalid literal for int() with base 10: ''](https://stackoverflow.com/questions/1841565/valueerror-invalid-literal-for-int-with-base-10) – chitown88 Dec 29 '18 at 10:50
  • Have you looked at [this question](https://stackoverflow.com/questions/1841565/valueerror-invalid-literal-for-int-with-base-10) it seems to be the same error when working with CSV files - perhaps try - `int(float(data_list[i][3]))` – jimbob88 Dec 29 '18 at 10:52
  • Thanks @chitown88, but im not so clear about what to do – Sankarshana Dec 29 '18 at 10:55
  • 3
    The error message is telling you that `int(data_list[i][3])` does not always have a valid number. (No matter what you believe it to contain). print out data[i][3] before your inner loop and you'll see when the issue occurs. – Paritosh Singh Dec 29 '18 at 10:57
  • Thanks @wowcha, using float as you suggested doesnt work either "ValueError: could not convert string to float: " – Sankarshana Dec 29 '18 at 10:57
  • @ParitoshSingh has the right idea. at some point it's coming back with "", which it can't convert. Find where that's happening, fix that, and try again. – chitown88 Dec 29 '18 at 10:59
  • @ParitoshSingh......i checked it before it self and it does correctly print out integer values – Sankarshana Dec 29 '18 at 11:03
  • @chitown88 thanks a lot......what you pointed out was actually the problem.....its done – Sankarshana Dec 29 '18 at 11:06
  • Without seeing your data this is fruitless. Your error is data-driven. You most probably have a column that is empty . – Patrick Artner Dec 29 '18 at 11:07
  • Yeah thanks everyone......its solved......i indeed had an empty column – Sankarshana Dec 29 '18 at 11:09
  • When programming if you run into an error, learn to question your assumptions. Assume you're wrong. The machine only does what you tell it to. Good luck programming. – Paritosh Singh Dec 29 '18 at 11:12
  • @Sankarshana, great! ParitoshSingh gave good advice. When you need to debug, insert some `print` statements so you can sort of see where it's messing up. Even better if you can use a debugger, where you can run code line by line, within the loop and see the values as they come in and out – chitown88 Dec 29 '18 at 11:15
  • @ParitoshSingh Thank you both a lot for your great advice. It will be helpful forever. – Sankarshana Dec 31 '18 at 16:29
  • @chitown88 than you too – Sankarshana Dec 31 '18 at 16:29

0 Answers0