Writing a list of tuples to a text file in Python
问题 I have a list of tuples in the format: ("some string", "string symbol", some number) For example, ("Apples", "=", 10) . I need to write them into the output file, like this: Apples = 10 I'm having trouble with the write method. How can it be done? 回答1: You can use: for t in some_list: f.write(' '.join(str(s) for s in t) + '\n') where f is your file . 回答2: list_of_tuples = [('Apples', '=', 10), ('Oranges', '<', 20)] f = open('file.txt', 'w') for t in list_of_tuples: line = ' '.join(str(x) for