Jorge Mendes
Jorge Mendes

Reputation: 97

python - write row by row tuples inside a list

My issue is to write what is in my file, row by row in my function.

Here's my code:

def read_operators_file(file_name):
"""Read a file with operators into a collection.

Requires: file_name, str with the name of a text file with a list of operators.
Ensures: list, with the operators in the file; each operators is a tuple with the various element 
concerning that operator, in the order provided in the file.
"""
inputFile1 = open("operators14h55.txt",'r+')
import constants
for i in range(constants.HEADER_TOTAL_LINES):  # read some lines to skip the header
    inputFile1.readline()
operators = []
for line in inputFile1:
    name, language, domain, last_hour, total_minutes = line.strip().split(', ')
    operators.append((name, language, domain, last_hour, total_minutes))
inputFile1.close()
return operators

that return's all in a row, and I want each tuple in a single row.

[('Henry Miller', 'english', 'laptops', 'premium', 3), ('François Greenwich', 'spanish', 'cameras', 'premium', 6), ('Ricardo Carvalho', 'portuguese', 'refrigerators', 'premium', 2)]

I want something similar to this:

[('Henry Miller', 'english', 'laptops', 'premium', 3), 
 ('François Greenwich', 'spanish', 'cameras', 'premium', 6), 
 ('Ricardo Carvalho', 'portuguese', 'refrigerators', 'premium', 2)]

Upvotes: 0

Views: 107

Answers (5)

Hari
Hari

Reputation: 11

for i in range(constants.HEADER_TOTAL_LINES):  # read some lines to skip the header
    inputFile1.readline()

I also dont understand why you have those two lines? Unless I've mistaken, if anyone cares to explain :) But those line can be made redundant completely.

Upvotes: 0

Anton vBR
Anton vBR

Reputation: 18916

This is not an answer but a remark. You can reduce your code drastically by using a list comprehension:

operators = []
for line in inputFile1:
    name, language, domain, last_hour, total_minutes = line.strip().split(', ')
    operators.append((name, language, domain, last_hour, total_minutes))

How about changing those 4 lines to:

operators = [tuple(line.strip().split(', ')) for line in inputFile1]

Or why not a full remake:


import constants # imports are always made first (very few exceptions)

def read_operators_file(file_name):
    """Read a file with operators into a collection.

    Requires: file_name, str with the name of a text file with a list of operators.
    Ensures: list, with the operators in the file; 
             each operators is a tuple with the various element 
             concerning that operator, in the order provided in the file.
    """

    with open(file_name,'r+') as inputFile1: # with ensures file closes, no need to worry
        # read some lines to skip the header
        for i in range(constants.HEADER_TOTAL_LINES):  
            next(inputFile1)
        operators = [tuple(line.strip().split(', ')) for line in inputFile1]

    return operators

# Function call
operators1 = read_operators_file("operators14h55.txt")

Now to your actual question. The operators is a list with tuples. You cannot format variables in Python. It is only when you print or write that you have that option.

Upvotes: 1

Foxan Ng
Foxan Ng

Reputation: 7151

You can use pprint.

from pprint import pprint

operators = [('Henry Miller', 'english', 'laptops', 'premium', 3), ('François Greenwich', 'spanish', 'cameras', 'premium', 6), ('Ricardo Carvalho', 'portuguese', 'refrigerators', 'premium', 2)]
pprint(source)

# results
[('Henry Miller', 'english', 'laptops', 'premium', 3),
 ('François Greenwich', 'spanish', 'cameras', 'premium', 6),
 ('Ricardo Carvalho', 'portuguese', 'refrigerators', 'premium', 2)]

Upvotes: 1

Adam W. Cooper
Adam W. Cooper

Reputation: 74

I think all you need to do is:

list_of_tuples = [(1,2),(4,5),(5,6)]
with open("out.txt") as new_file:
    new_file.write("\n".join( str(tuple) for tuple in list_of_tuples)

Upvotes: 0

Van Peer
Van Peer

Reputation: 2167

Sample: this writes each tuple in separate lines to xyz.txt

l=[('Henry Miller', 'english', 'laptops', 'premium', 3), ('François Greenwich', 'spanish', 'cameras', 'premium', 6), ('Ricardo Carvalho', 'portuguese', 'refrigerators', 'premium', 2)]

with open('xyz.txt', 'w') as f:
    for x in l:
        f.write(str(x) + "\n")

Upvotes: 0

Related Questions