2011-09-14 3 views
2

값 간의 탭 튜플 인쇄 :제가 아래와 같은 텍스트 파일을

this000is00a00test001251!! 
this000is00a00test001251!! 
this000is00a00test001251!! 
this000is00a00test001251!! 

I 그것을 통해 파싱 다음 코드가 : 여기

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

의 출력을 나는 인터프리터에서 얻을 :

('this', 'is', 'a', 'test', '1251') 
('this', 'is', 'a', 'test', '1251') 
('this', 'is', 'a', 'test', '1251') 
('this', 'is', 'a', 'test', '1251') 

은 내가보고 싶은 것은 다음과 같은 형식의 텍스트입니다 :

this is a test 1251 

또는

this,is,a,test,1251 

어떤 아이디어? 또는 3.0에서 텍스트 서식을 설명하는 좋은 링크가 있습니까?

감사합니다. 당신이 문자열 목록에 가입하려면

답변

12

, 당신과 같이 join()를 사용할 수 있습니다

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

출력 : 쉼표를 들어

one two three 

을, 단지 ",".join"\t".join를 교체합니다. 조인은 예제 코드에서 사용 된 튜플과도 작동합니다 (반복 가능한 모든 iterable에서 작동).

+1

다른 방법으로 값의 형식을 지정하려면 [형식 문자열 구문] (http://docs.python.org/library/string.html#formatstrings)을 참조하십시오. – agf

+1

감사합니다. 그게 효과가 있었어! –

관련 문제