Я печатаю имя столбца, тип данных и максимальную длину столбцов из файла Excel.
Ниже приведен код и выходные данные, необходимо экспортировать выходные данные в файл json.
Файл Excel (assign..xlsx):
<html>
<table >
<thead>
<tr>
<th>Type</th>
<th>Amount received</th>
<th>Currency</th>
<th>Flag</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>Educationsl</td>
<td>1422.00</td>
<td>USD</td>
<td>2018-11-30</td>
</tr>
</tbody>
</table>
</html>
Выход
Column Name : Type
Data type : String
Size : 11
Column Name : Amountreceived
Data type : Float
Size : 6
Column Name : Currency
Data type : String
Size : 3
Column Name : Flag
Data type : String
Size : 1
Column Name : Date
Data type : Float
Date format : ddmmyyyy
Код
import string
import re
import datetime
import xlrd
loc = r"C:\Users\Documents\PythonStuff\assignment.xlsx"
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
for i in range(sheet.ncols):
print ("Column Name : " + re.sub('[^A-Za-z0-9]+', '', sheet.cell_value(0, i).replace(' ','_')))
##Data Type
if str(sheet.cell_type(1,i)) == '1':
print ("Data type : String ")
elif isinstance(sheet.cell_value(1,i), float) == True:
print ("Data type : Float ")
elif str(sheet.cell_type(1,i)) =='3':
print ("Data type : Date ")
## Date Format
if str(sheet.cell_type(1,i)) =='3':
print ("Date format : " + "ddmmyyyy" +"\n ")
else:
print ("Size : " + str(len (str(sheet.cell_value(1,i))))+"\n ")
Желаемый вывод
{"Excel": [{"Type": "Educational", "Amount received": "1422.00", "Currency": "USD", "Flag": "N", "Date": "2018-11-30"} ]}