我沒有通過搜索和等待答覆得到這個問題的良好答案,我認爲這是因爲我的問題的第一個迭代措辭不佳。向製表符分隔的文件添加標題和新列
所有我想要做的是兩列添加到類似如下的文件:
name phone email website
Diane Grant Albrecht M.S.
Lannister G. Cersei M.A.T., CEP 111-222-3333 [email protected] www.got.com
Argle D. Bargle Ed.M.
Sam D. Man Ed.M. 000-000-1111 [email protected] www.daManWithThePlan.com
Sam D. Man Ed.M.
Sam D. Man Ed.M. 111-222-333 [email protected] www.daManWithThePlan.com
D G Bamf M.S.
Amy Tramy Lamy Ph.D.
因此,它看起來像這樣:
name phone email website area degrees
D G Albright M.A.
Lannister G. Cersei 111-222-3333 [email protected] www.got.com CEP M.A.T.
Argle D. Bargle Ed.M.
Sam D. Man 000-000-1111 [email protected] www.daManWithThePlan.com Ed.M.
Sam D. Man Ed.M.
Sam D. Man 111-222-333 [email protected] www.daManWithThePlan.com Ed.M.
D G Bamf M.S.
Amy Tramy Lamy
儘管「writeheader() '在幾篇文章中提到,當我通過它的字段名列表時,它會返回一個錯誤。
TypeError: writeheader() takes exactly 1 argument (2 given)
logout
這是我到目前爲止有:
with open('ieca_first_col_fake_text.txt','r') as input:
with open('new_col_dict.txt','w') as output:
dict_writer = csv.DictWriter(output, fieldnames, delimiter = '\t')
dict_reader = csv.DictReader(input, delimiter = '\t')
#dict_writer.writeheader(fieldnames)
for row in dict_reader:
print row
這不會產生有頭一個txt文件...
那麼錯誤是什麼? –
請注意,如果輸入和輸出具有相同的預期壽命,['with a in,b as out:'](http://stackoverflow.com/a/4617069/1176601)將使其更具可讀性 – Aprillion