Traceback from a Python Script: invalid literal

84 views Asked by At

In short, what the Python script is supposed to do is to load and calculate ASCII type files.

With some previously pre-processed files, it works without errors, while with mine it throws an error. In any case, it looks as though my file is different from what it should be (input-wise).

Traceback (most recent call last):
  File "D:\TER\Scripts python\PuissantPDI.py", line 124, in <module>
    for i in range (42, (42+(int(type_ncols)*int(type_nrows)))):
ValueError: invalid literal for int() with base 10: 'nrows'

*It is not run in any QGIS/ArcGIS software, just from the cmd or IDLE.

EDIT

Just a small part of the code:

import sys 

print("\nPDI Processing...\n")

''' OPTION FILE '''

with open("options_PDI.txt") as f:
    content = f.readlines()
content = [x.strip('\n') for x in content]
option= []
for elem in content:
    option.extend(elem.strip().split(" "))
f.close()

b_type_file=option[1]
b_totalstage_file=option[3]
b_function_file=option[5]
b_state_file=option[7]
b_age_file=option[9]
b_material_file=option[11]
b_occstage_file=option[13]
landcover_file=option[15]
landuse_file=option[17]
transport_file=option[19]

print("Option file loaded...\n")

''' BUILDING TYPE FILE '''

with open(b_type_file) as f:
    content = f.readlines()
content = [x.strip('\n') for x in content]
b_type= []
for elem in content:
    b_type.extend(elem.strip().split(" "))
f.close()

type_ncols=b_type[9]
type_nrows=b_type[19]
type_xll=b_type[25]
type_yll=b_type[31]
type_pixelsize=b_type[38]
type_nodata=b_type[41]

type_value=[]
for i in range (42, (42+(int(type_ncols)*int(type_nrows)))):
    type_value.append(b_type[i])

print("Building type file loaded...")
2

There are 2 answers

8
Martijn Pieters On BEST ANSWER

You are splitting on single spaces:

option= []
for elem in content:
    option.extend(elem.strip().split(" "))

You have an extra space somewhere, so all your offsets are off-by-one.

You could solve that by simply *removing the argument to str.split(). The text will then automatically be stripped, and split on arbitrary width whitespace. It won't matter if there are 1 or 2 or 20 spaces in the file then:

with open("options_PDI.txt") as f:
    option = f.read().split()

Note that I don't even bother with splitting the file into lines or stripping away the newlines.

Note that your treatment of the files is rather fragile still; you are expecting certain values to exist at certain positions. If your files contain label value style lines, you can just read the whole file into a dictionary:

with open("options_PDI.txt") as f:
    options = dict(line.strip().split(None, 1) for line in f if ' ' in line)

and use that dictionary to address various values:

type_ncols = int(options['ncols'])
1
James Mills On

The error is quote obvious here.

'nrows' cannot be converted to an int.

For some reason however you are calling this type_rows is not a string representation of a valid integer and seems to contain the string 'nrows' instead.


For example:

>>> type_rows = "nrows"
>>> int(type_rows)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'nrows'
>>>