我正在從CSV文件的特定值中創建制表符分隔文件。 CSV文件包含上個月所有訂單,我需要正確格式化才能導入當前使用的會計軟件。下面是數據從CSV文件中的示例:將CSV轉換爲TDV - 格式問題
Customer Order Number Item Line Number Quantity Product Description
cust1 Order #1 1 40 desc1
cust1 Order #2 1 101 desc2
cust2 Order #3 1 3 desc3
cust2 Order #3 2 8 desc3
cust2 Order #3 3 8 desc3
cust1 Order #4 1 75 desc4
現在,每個系列的Order Number
我需要創建TDV文件看起來像這樣的一個部分:(忽略括號,這些都只是以顯示數值來自上面)
1 cust1 HA5ZV1 Desc1 Due Date ...
2 1 (Item #) 40 (Qty) ... ... ...
1 cust1 HA6A17 Desc2 Due Date ...
2 1 (Item #) 101 (Qty) ... ... ...
1 cust2 HA6AM1 Desc3 Due Date ...
2 1 (Item #) 3 (Qty) ... ... ...
2 2 (Item #) 8 (Qty) ... ... ...
2 3 (Item #) 8 (Qty) ... ... ...
希望這是有道理的。到目前爲止,我所做的是從原始CSV文件中創建一個字典,但對如何遍歷我的字典並將標題(標有「1」的行)寫入一次感到困惑,然後將值寫入(標有「2」的行)每次出現相同的Order Number
。這是我到目前爲止的代碼:
data = csv.reader(open(import_dir))
fields = data.next()
new_file = export_dir+os.path.basename(import_dir)
tab_file = open(export_dir+os.path.basename(import_dir), 'a+')
for row in data:
items = zip(fields, row)
item = {}
for (name, value) in items:
item[name] = value.strip()
tab_file.write('1\t'+item['Customer']+'\t'+item['Order Number']+'\t'
+item['Product Description']+'\t'+item['Due Date']+'\n'+
'2\t'+item['Item Line Number']+'\t'+item['Quantity']+'\t'+
...
但這種代碼把標題數據每行一個項目之前,而不是將其僅在每個訂單的開始。如果每個客戶只訂購一件商品,那就沒有問題,但由於某些訂單有多個訂單項,因此會導致格式化。如果任何人都能指出我會朝着正確的方向發展,那將是很棒的。謝謝!
使用'csv.writer(tab_file,delimiter ='\ t')'它會寫入TSV文件.. –