python - 在列表中逐行写入元组

时间:2017-11-16 14:04:33

标签: python

我的问题是在我的函数中逐行写出我文件中的内容。

这是我的代码:

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

返回所有行,我希望每个元组都在一行中。

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

我想要类似的东西:

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

5 个答案:

答案 0 :(得分:1)

您可以使用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)]

答案 1 :(得分:1)

这不是一个答案,而是一个评论。您可以使用列表解析来大幅减少代码:

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

如何将这4行更改为:

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

或者为什么不完整翻拍:

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")

现在到你的实际问题。运算符是带元组的列表。你不能在Python中格式化变量。只有在您打印或写入时才有选择权。

答案 2 :(得分:0)

示例:这会将每个元组分开写入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")

答案 3 :(得分:0)

我认为您需要做的就是:

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)

答案 4 :(得分:0)

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

我也不明白为什么你有这两行?除非我弄错了,否则如果有人愿意解释:)但这些行可以完全变得冗余。