我意識到有一種方法可以使用'awk'添加一列。將帶有標題的列添加到製表符分隔的文本文件中?
但是我對這個選擇不太熟悉,所以我想問一下是否有一種方法可以使用Python將列添加到製表符分隔的文本文件中?
具體而言,這裏是我需要在添加一列的情景:
我有以下格式(我知道看着它的格式可能不那麼清楚,但電話,電子郵件和數據網站對應不同的列):
name phone email website
D G Albright 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.
而我正在爲第一列寫一個解析器。我想將「練習領域」添加到名爲「區域」的新列中,在這種情況下,前者將是「CEP」。我遍歷該文件,並使用彈出功能將區域與第一列的其餘部分分開。然後我將它添加到一個列表中,該列表只是在函數中死掉,因爲它沒有添加到電子表格中。
這裏是我的腳本:
def parse_ieca_gc(s):
### HANDLE NAME ELEMENT ######
degrees = ['M.A.T.','Ph.D.','MA','J.D.',
'Ed.M.', 'M.A.', 'M.B.A.',
'Ed.S.', 'M.Div.', 'M.Ed.',
'RN', 'B.S.Ed.', 'M.D.', 'M.S.']
degrees_list = []
# check whether the name string has
# an area of practice by
# checking if there's a comma separator
if ',' in s['name']:
# separate area of practice from name
# and degree and bind this to var 'area'
split_area_nmdeg = s['name'].split(',')
area = split_area_nmdeg.pop()
# Split the name and deg by spaces.
# If there's a deg, it will match with one
# of elements and will be stored deg list.
# The deg is removed name_deg list
# and all that's left is the name.
split_name_deg = re.split('\s',split_area_nmdeg[0])
for word in split_name_deg:
for deg in degrees:
if deg == word:
degrees_list.append(split_name_deg.pop())
name = ' '.join(split_name_deg)
預計輸出
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 Ph.D.
此代碼也無法正常工作:
fieldnames = ['name','degrees','area','phone','email','website']
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
dict_writer.writerow(fieldnames)
dict_writer.writerow(row)
什麼是預期的輸出? –
[如何使用Python將新列添加到CSV文件?](http://stackoverflow.com/questions/11070527/how-to-add-a-new-column-to-a-csv- file-using-python) – Daenyth
在這種情況下,ex是'CEP',是什麼意思,是一個名爲'area'的新列。 – Oz123