-1

I am using jupyter python 3. I have tried to import data from .tsp file but it keeps showing me this error.And I saw some people had same problem and they solved it thanks to convert, but it did not work on my codes.

NAME: berlin52
TYPE: TSP
COMMENT: 52 locations in Berlin (Groetschel)
DIMENSION : 52
EDGE_WEIGHT_TYPE : EUC_2D
NODE_COORD_SECTION
1 565.0 575.0
2 25.0 185.0
3 345.0 750.0
4 945.0 685.0
5 845.0 655.0
6 880.0 660.0
7 25.0 230.0
8 525.0 1000.0
9 580.0 1175.0
10 650.0 1130.0
# Open input file
infile = open(r'C:\Users\13136\OneDrive\Desktop\AI\berlin52.tsp')

# Read instance header
Name = infile.readline().strip().split()[1] # NAME
FileType = infile.readline().strip().split()[1] # TYPE
Comment = infile.readline().strip().split()[1] # COMMENT
Dimension = infile.readline().strip().split()[1] # DIMENSION
EdgeWeightType = infile.readline().strip().split()[1] # EDGE_WEIGHT_TYPE
infile.readline()

# Read node list
nodelist = []
N = int(Dimension)
for i in range(0, int(Dimension)):
    x,y = infile.readline().strip().split()[1:]
    nodelist.append([float(x), float(y)])

# Close input file
infile.close()
ValueError                                Traceback (most recent call last)
<ipython-input-22-5e3fe725955a> in <module>
     12 # Read node list
     13 nodelist = []
---> 14 N = int(Dimension)
     15 for i in range(0, int(Dimension)):
     16     x,y = infile.readline().strip().split()[1:]

ValueError: invalid literal for int() with base 10: ':'
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
gn khrmn
  • 23
  • 1
  • 6

1 Answers1

1
Name = infile.readline().strip().split(':')[1] # NAME
FileType = infile.readline().strip().split(':')[1] # TYPE
Comment = infile.readline().strip().split(':')[1] # COMMENT
Dimension = infile.readline().strip().split(':')[1] # DIMENSION
EdgeWeightType = infile.readline().strip().split(':')[1] # EDGE_WEIGHT_TYPE

The two lines for DIMENSION and EDGE_WEIGHT_TYPE in your file do not have the : immediately following the name, but have some extra space inbetween, so split() will split these lines at each space, into three parts, e.g.:

['DIMENSION', ':', '52']

You are selecting the second part, which cannot be interpreted as int. You want to always have the second part of the line after splitting the line by :, not by , which split(':') does for you, e.g.:

['DIMENSION ', ' 52']

The extra whitespaces could be removed with a .strip() call after these lines, but int will also accept it without.

Dimension = infile.readline().split(':')[1].strip()

This will still cut of fields containing extra :, but I suppose such special cases are not that important to you here.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • Thank you. And now i got another problem which is that : '''ValueError Traceback (most recent call last) in 15 N = int(Dimension) 16 for i in range(0, int(Dimension)): ---> 17 x,y = infile.readline().strip().split()[1:] 18 nodelist.append([float(x), float(y)]) 19 ValueError: not enough values to unpack (expected 2, got 0) ''' – gn khrmn Sep 01 '19 at 18:52
  • @gnkhrmn You are iterating that `for` loop `Dimension` times, but there aren't that many lines in the file, so this cannot work. – walnut Sep 01 '19 at 18:57
  • So there is no way I can import just 10 line data? How can I solve this issue? Thanks in advance. – gn khrmn Sep 01 '19 at 19:01
  • @gnkhrmn https://stackoverflow.com/questions/29022377/how-to-while-loop-until-the-end-of-a-file-in-python-without-checking-for-empty-l/29022395 – walnut Sep 01 '19 at 19:06