ascii - Traceback from a Python Script: invalid literal -
in short, python script supposed load , calculate ascii type files.
with pre-processed files, works without errors, while mine throws error. in case, looks though file different should (input-wise).
traceback (most recent call last): file "d:\ter\scripts python\puissantpdi.py", line 124, in <module> in range (42, (42+(int(type_ncols)*int(type_nrows)))): valueerror: invalid literal int() base 10: 'nrows'
*it not run in qgis/arcgis software, cmd or idle.
edit
just small part of code:
import sys print("\npdi processing...\n") ''' option file ''' open("options_pdi.txt") f: content = f.readlines() content = [x.strip('\n') x in content] option= [] 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 ''' open(b_type_file) f: content = f.readlines() content = [x.strip('\n') x in content] b_type= [] 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=[] in range (42, (42+(int(type_ncols)*int(type_nrows)))): type_value.append(b_type[i]) print("building type file loaded...")
you splitting on single spaces:
option= [] elem in content: option.extend(elem.strip().split(" "))
you have space somewhere, offsets off-by-one.
you solve *removing argument str.split()
. text automatically stripped, , split on arbitrary width whitespace. won't matter if there 1 or 2 or 20 spaces in file then:
with open("options_pdi.txt") f: option = f.read().split()
note don't bother splitting file lines or stripping away newlines.
note treatment of files rather fragile still; expecting values exist @ positions. if files contain label value
style lines, can read whole file dictionary:
with open("options_pdi.txt") f: options = dict(line.strip().split(none, 1) line in f if ' ' in line)
and use dictionary address various values:
type_ncols = int(options['ncols'])
Comments
Post a Comment