2017-10-11 79 views
1

我需要將csv規範文件轉換爲YAML文件以滿足項目需要。我爲它寫了一小段python代碼,但不能按預期工作。我無法使用任何在線轉換器,因爲我正在工作的客戶端不會接受該轉換器。這裏是Python代碼,我有:使用Python腳本CSV到Yaml蓋度

import csv 
csvfile = open('custInfo.csv', 'r') 

datareader = csv.reader(csvfile, delimiter=',', quotechar='"') 
data_headings = [] 

yaml_pretext = "sourceTopic : 'BIG_PARTY'" 
yaml_pretext += "\n"+'validationRequired : true'+"\n" 
yaml_pretext += "\n"+'columnMappingEntityList :'+"\n" 
for row_index, row in enumerate(datareader): 
    if row_index == 0: 
     data_headings = row 
    else: 
     # new_yaml = open('outfile.yaml', 'w') 
     yaml_text = "" 
     for cell_index, cell in enumerate(row): 
      lineSeperator = " " 
      cell_heading = data_headings[cell_index].lower().replace(" ", "_").replace("-", "") 
      if (cell_heading == "source"): 
       lineSeperator = ' - ' 

      cell_text = lineSeperator+cell_heading + " : " + cell.replace("\n", ", ") + "\n" 

      yaml_text += cell_text 
     print yaml_text 

csvfile.close() 

CSV文件中有4列,那就是:

source    destination  type  childFields 
fra:AppData   app_data   array application_id,institute_nm 
fra:ApplicationId application_id  string null 
fra:InstituteName institute_nm  string null 
fra:CustomerData  customer_data  array name,customer_address,telephone_number 
fra:Name    name    string null 
fra:CustomerAddress customer_address array street,pincode 
fra:Street   street    string null 
fra:Pincode   pincode   string null 
fra:TelephoneNumber telephone_number string null 

這裏是YAML文件中我得到的輸出

- source : fra:AppData 
    destination : app_data 
    type : array 
    childfields : application_id,institute_nm 

    - source : fra:ApplicationId 
    destination : application_id 
    type : string 
    childfields : null 

    - source : fra:InstituteName 
    destination : institute_nm 
    type : string 
    childfields : null 

    - source : fra:CustomerData 
    destination : customer_data 
    type : array 
    childfields : name,customer_address,telephone_number 

    - source : fra:Name 
    destination : name 
    type : string 
    childfields : null 

    - source : fra:CustomerAddress 
    destination : customer_address 
    type : array 
    childfields : street,pincode 

    - source : fra:Street 
    destination : street 
    type : string 
    childfields : null 

    - source : fra:Pincode 
    destination : pincode 
    type : string 
    childfields : null 

    - source : fra:TelephoneNumber 
    destination : telephone_number 
    type : string 
    childfields : null 

當類型是數組時,我需要輸出爲childField,而不是新行。所以期望的輸出將是:

- source : fra:AppData 
    destination : app_data 
    type : array 
    childfields : application_id,institute_nm 
     - source : fra:ApplicationId 
     destination : application_id 
     type : string 
     childfields : null 

     - source : fra:InstituteName 
     destination : institute_nm 
     type : string 
     childfields : null 

    - source : fra:CustomerData 
    destination : customer_data 
    type : array 
    childfields : name,customer_address,telephone_number 
     - source : fra:Name 
     destination : name 
     type : string 
     childfields : null 

     - source : fra:CustomerAddress 
     destination : customer_address 
     type : array 
     childfields : street,pincode 
      - source : fra:Street 
      destination : street 
      type : string 
      childfields : null 

      - source : fra:Pincode 
      destination : pincode 
      type : string 
      childfields : null 

     - source : fra:TelephoneNumber 
     destination : telephone_number 
     type : string 
     childfields : null 

任何人都可以請幫我我怎麼能得到這個?感謝您的幫助提前

克里希納

+0

所以,你需要兩個主頭只 - 應用程序數據或CustomerData? –

+0

不完全。它不是關於擁有主標題,如果類型是數組,它將有子字段。那麼子字段將會有一些縮進 – user3444971

回答

1

您目前沒有使用任何YAML庫生成輸出

感謝。這是不好的做法,因爲你不檢查你輸出的字符串內容是否包含需要引用的YAML特殊字符。

接下來,這是無效的YAML:

childfields : application_id,institute_nm 
     - source : fra:ApplicationId 
     destination : application_id 
     type : string 
     childfields : null 

childfields不可兼得的標量值(application_id,institute_nm)和序列值(開始與該項目- source : fra:ApplicationId)。

嘗試生成列表和類型的字典您的結構,然後轉儲結構:

import yaml,csv 

csvfile = open('custInfo.csv', 'r') 
datareader = csv.reader(csvfile, delimiter=",", quotechar='"') 
result = list() 
type_index = -1 
child_fields_index = -1 

for row_index, row in enumerate(datareader): 
    if row_index == 0: 
    # let's do this once here 
    data_headings = list() 
    for heading_index, heading in enumerate(row): 
     fixed_heading = heading.lower().replace(" ", "_").replace("-", "") 
     data_headings.append(fixed_heading) 
     if fixed_heading == "type": 
     type_index = heading_index 
     elif fixed_heading == "childfields": 
     child_fields_index = heading_index 
    else: 
    content = dict() 
    is_array = False 
    for cell_index, cell in enumerate(row): 
     if cell_index == child_fields_index and is_array: 
     content[data_headings[cell_index]] = [{ 
      "source" : "fra:" + value.capitalize(), 
      "destination" : value, 
      "type" : "string", 
      "childfields" : "null" 
      } for value in cell.split(",")] 
     else: 
     content[data_headings[cell_index]] = cell 
     is_array = (cell_index == type_index) and (cell == "array") 
    result.append(content) 
print yaml.dump(result)