diff --git a/tsplib95/fields.py b/tsplib95/fields.py index 7de9b88..8b1f7eb 100644 --- a/tsplib95/fields.py +++ b/tsplib95/fields.py @@ -369,7 +369,7 @@ def render(self, tours): tour_string = ' '.join(str(i) for i in tour) tour_strings.append(f'{tour_string} -1') - if tour_strings: + if len(tour_strings) > 1: tour_strings += ['-1'] return '\n'.join(tour_strings) diff --git a/tsplib95/models.py b/tsplib95/models.py index 1eb7223..a560671 100644 --- a/tsplib95/models.py +++ b/tsplib95/models.py @@ -99,7 +99,7 @@ def parse(cls, text, **options): # prepare the regex for all known keys keywords = '|'.join(cls.fields_by_keyword) sep = r'''\s*:\s*|\s*\n''' - pattern = f'({keywords}|EOF)(?:{sep})' + pattern = f'({keywords}|EOF)(?:{sep})|(?:EOF$)' # split the whole text by known keys regex = re.compile(pattern, re.M) @@ -112,7 +112,7 @@ def parse(cls, text, **options): # parse into a dictionary data = {} for keyword, value in zip(field_keywords, field_values): - if keyword != 'EOF': + if keyword != 'EOF' and keyword is not None: field = cls.fields_by_keyword[keyword] name = cls.names_by_keyword[keyword] data[name] = field.parse(value.strip())