使用值之间的选项卡打印元组

Lan*_*ins 2 python formatting text file

我有一个类似下面的文本文件:

this000is00a00test001251!!
this000is00a00test001251!!
this000is00a00test001251!!
this000is00a00test001251!!
Run Code Online (Sandbox Code Playgroud)

我有以下代码来解析它:

def file_open():
    my_file = open(r'C:\Users\test\Desktop\parse_me.txt','r', encoding='cp1252')
    return my_file

def parse(current_line):
    seq_1 = (current_line[0:4])
    seq_2 = (current_line[7:9])
    seq_3 = (current_line[11:12])
    seq_4 = (current_line[14:18])
    seq_5 = (current_line[20:24])
    return(seq_1, seq_2, seq_3, seq_4, seq_5)

def export_file(current_file):
    for line in current_file:
        x = parse(line)
        print (x)

export_file(file_open())
Run Code Online (Sandbox Code Playgroud)

这是我在解释器中得到的输出:

('this', 'is', 'a', 'test', '1251')
('this', 'is', 'a', 'test', '1251')
('this', 'is', 'a', 'test', '1251')
('this', 'is', 'a', 'test', '1251')
Run Code Online (Sandbox Code Playgroud)

我想看到的是这样格式的文本:

this    is    a    test    1251
Run Code Online (Sandbox Code Playgroud)

要么

this,is,a,test,1251
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?或者你有什么好的链接解释3.0中的文本格式?

谢谢!

Joh*_*yon 13

如果要加入字符串列表,可以这样使用join():

list_of_strings = ['one', 'two', 'three']
print "\t".join(list_of_strings) #\t is the tab character
Run Code Online (Sandbox Code Playgroud)

输出:

one    two    three
Run Code Online (Sandbox Code Playgroud)

对于逗号,只需替换"\t".join",".join.Join也可以使用示例代码中使用的元组(它适用于任何可迭代的).