1

txt file

Zurich, Sun, (8, 52)
Auckland, Sun, (20, 52)
Barcelona, Sun, (8, 52)
Vancouver, Sun, (0, 52)
Beirut, Sun, (9, 52)
Athens, Sun, (9, 52)
Amsterdam, Sun, (8, 520)
Toronto, Sun, (2, 52)
Vienna, Sun, (8, 52)
Winnipeg, Sun, (1, 52)
Atlanta, Sun, (2, 52)
Anchorage, Sat, (23, 52)
Warsaw, Sun, (8, 52)
Ankara, Sun, (10, 52)
Washington DC, Sun, (2, 52)

code:

list_aldskjfa = []
data = open('city_time.txt', 'r')
data = data.read()
list1 = []
for line in sorted(open('A1_cities_and_time.txt')):
    list1.append(line)
    print(line, end='')
print(list1)
output: ['Amsterdam, Sun, (8, 520)\n', 'Anchorage, Sat, (23, 52)\n', 'Ankara, Sun, (10, 52)\n', 'Athens, Sun, (9, 52)\n', 'Atlanta, Sun, (2, 52)\n', 'Auckland, Sun, (20, 52)\n', 'Barcelona, Sun, (8, 52)\n', 'Beirut, Sun, (9, 52)\n', 'Toronto, Sun, (2, 52)\n', 'Vancouver, Sun, (0, 52)\n', 'Vienna, Sun, (8, 52)\n', 'Warsaw, Sun, (8, 52)\n', 'Washington DC, Sun, (2, 52)', 'Winnipeg, Sun, (1, 52)\n', 'Zurich, Sun, (8, 52)\n']

As you can see, the list is alphabetized properly, with amsterdam, then anchorage, then ankara, etc. The problem is that the entire thing is a string - including the tuple and the date. The ideal output would look like [('Amsterdam', 'Sun', (8,520)), ('Anchorage', 'Sun', (8,520)), etc.]

  • 3
    Take a look at this library https://docs.python.org/3/library/csv.html you will be able to read it in more easely and then convert the strings to the appropriate types. – Robin Dillen Jan 11 '22 at 20:29

2 Answers2

1

It seems as though what you need to do is split each entry of the list into a tuple. One nice solution is to use the parse module. Here's a way to do it from scratch.

def sep(line):
    line = line.strip()
    parts = line.split(', ',maxsplit = 2)
    return tuple(parts[:2] + [eval(parts[2])])

output = list(map(sep,list1))

To include the the file reading in the code, we could proceed as follows.

def sep(line):
    line = line.strip()
    parts = line.split(', ',maxsplit = 2)
    return tuple(parts[:2] + [eval(parts[2])])

with open('list.txt','r') as f:
    output = list(map(sep,f.readlines()))
output.sort()
Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16
1

Note that this solution assumes you convert the text file to a csv (just rename the ending). Here is an alternative implementation to get the last element as a tuple:

import csv, ast

output = []
with open('path/to/city_time.csv', newline='') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
        for row in spamreader:
            row = row[:2] + [",".join(row[2:])]
            output.append(row)


output = sorted(output)

for o in output:
    o[2] = ast.literal_eval(o[2].strip())

print(output)

Output:

[['Amsterdam', ' Sun', (8, 520)], ['Anchorage', ' Sat', (23, 52)], ['Ankara', ' Sun', (10, 52)], ['Athens', ' Sun', (9, 52)], ['Atlanta', ' Sun', (2, 52)], ['Auckland', ' Sun', (20, 52)], ['Barcelona', ' Sun', (8, 52)], ['Beirut', ' Sun', (9, 52)], ['Toronto', ' Sun', (2, 52)], ['Vancouver', ' Sun', (0, 52)], ['Vienna', ' Sun', (8, 52)], ['Warsaw', ' Sun', (8, 52)], ['Washington DC', ' Sun', (2, 52)], ['Winnipeg', ' Sun', (1, 52)], ['Zurich', ' Sun', (8, 52)]]
Richard K Yu
  • 2,152
  • 3
  • 8
  • 21