2016-12-09 2 views
0

Python docx 모듈을 사용하여 특정 테이블 데이터 집합을 구문 분석하려고합니다.docx를 사용하여 사전 형식으로 테이블 데이터 구문 분석

테이블 데이터는 내가 추가 처리를 위해 데이터를 사용할 수 있도록 키 값 형식으로 "기관"을 각각 "버전"을 검색해야이 enter image description here

같이 보입니다. 내가 사용하는 경우

나는 사전을 반복 드릴 수 없습니다 - 나에게주는

d = OrderedDict(zip(table.cell(rowNo, 0).text, table.cell(rowNo, 2).text)) 

을 orderedDictionary하지만 난 나에게 4.5.6

from docx import Document 

document = Document('myfile.docx') 

    for table in document.tables: 
     printTable = False 
     rowNo = 0; 
     for row in table.rows: 
      for cell in row.cells: 
       if cell.text == "Table2": 
        printTable = False 
      if printTable: 
       print (table.cell(rowNo, 0).text + '=' + table.cell(rowNo, 2).text) 
      for cell in row.cells: 
       if cell.text == "Authorities": 
        printTable = True 
      rowNo += 1 
를주고 내가 기대하고 d['Juno'] 를 사용하여 값에 액세스하지 못할

구문 분석 후 아래의 형식으로 데이터를 가져옵니다. -

Juno=4.5.6 
Acrux=3.5.6 
Mars=5.6.7 

답변

1

당신은 사전을 정의하고 달성 할 수있는이 -

from docx import Document 

document = Document('myfile.docx') 
data = {} 
for table in document.tables: 
    printTable = False 
    rowNo = 0; 
    for row in table.rows: 
     for cell in row.cells: 
      if cell.text == "Table2": 
       printTable = False 
     if printTable: 
      data[table.cell(rowNo, 0).text] = table.cell(rowNo, 2).text 
     for cell in row.cells: 
      if cell.text == "Authorities": 
       printTable = True 
     rowNo += 1 
print (data) 

사전 형식으로 당신에게 예상되는 데이터를 줄 것이다