for each_row in row_list :
if not all(map(len, each_row)) :
continue
else :
UICData.append(row_list)
但是这不能正常工作,因为我收到错误:
文件 “/Users/kenmarold/PycharmProjects/sweetCrude/Work/sweetCrude.py”,我不太确定如何解决这个问题,下一步的方向是什么?我还在下面附上了完整的脚本。线 56、在PrepareRawData
如果不是全部(地图(len,each_row)):
类型错误:“浮动”对象不可迭代
#!/usr/bin/env python3
import os
import sqlite3
import csv
import unicodecsv
from datetime import date
from xlrd import open_workbook, xldate_as_tuple
from xlwt import Workbook
orig_xls = 'data/all_uic_wells_jun_2016.xls'
temp_xls = 'data/temp.xls'
new_csv = 'data/gh_ready_uic_well_data.csv'
temp_csv = 'data/temp.csv'
input_worksheet_index = 0 # XLS Sheet Number
output_workbook = Workbook()
output_worksheet = output_workbook.add_sheet('Sweet Crude')
lat_col_index = 13
long_col_index = 14
#### SELECT AND FORMAT DATA
def PrepareRawData(inputFile, tempXLSFile, tempCSVFile, outputFile):
# 0 = API# # 7 = Approval Date
# 1 = Operator # 13 = Latitude
# 2 = Operator ID # 14 = Longitude
# 3 = Well Type # 15 = Zone
keep_columns = [0, 1, 2, 3, 7, 13, 14, 15]
with open_workbook(inputFile) as rawUICData:
UICSheet = rawUICData.sheet_by_index(input_worksheet_index)
UICData = []
for each_row_index in range(1, UICSheet.nrows - 1, 1):
row_list = []
lat_num = UICSheet.cell_value(each_row_index, lat_col_index) # Get Lat Values
long_num = UICSheet.cell_value(each_row_index, long_col_index) # Get Long Values
if lat_num != 0.0 and long_num != 0.0: # Find Zero Lat/Long Values
for each_column_index in keep_columns:
cell_value = UICSheet.cell_value(each_row_index, each_column_index)
cell_type = UICSheet.cell_type(each_row_index, each_column_index)
if cell_type == 3:
date_cell = xldate_as_tuple(cell_value, rawUICData.datemode)
date_cell = date(*date_cell[0:3]).strftime('%m/%d/%Y')
row_list.append(date_cell)
else:
row_list.append(cell_value)
for each_row in row_list :
if not all(map(len, each_row)) :
continue
else :
UICData.append(row_list)
# CreateDB(row_list) # Send row data to Database
for each_list_index, output_list in enumerate(UICData):
for each_element_index, element in enumerate(output_list):
output_worksheet.write(each_list_index, each_element_index, element)
output_workbook.save(tempXLSFile)
#### RUN XLS-CSV CONVERSION
workbook = open_workbook(tempXLSFile)
sheet = workbook.sheet_by_index(input_worksheet_index)
fh = open(outputFile, 'wb')
csv_out = unicodecsv.writer(fh, encoding = 'utf-8')
for each_row_number in range(sheet.nrows) :
csv_out.writerow(sheet.row_values(each_row_number))
fh.close()
#### KILL TEMP FILES
filesToRemove = [tempXLSFile]
for each_file in filesToRemove:
os.remove(each_file)
print("Raw Data Conversion Ready for Grasshopper")
# ---------------------------------------------------
PrepareRawData(orig_xls, temp_xls, temp_csv, new_csv)
# ---------------------------------------------------
for each_row in row_list :
if not isinstance(each_row, list):
each_row = [each_row]
if not any(map(len, each_row)) :
continue
UICData.append(row_list)
编辑:如果any/map/len仍然引发它,那么我会尝试不同的路线来检查它是否为空。
我也不确定为什么要附加整个 row_list 而不是当前行。我将其更改为附加each_row。
选项1
for each_row in row_list:
if not each_row:
continue
UICData.append(each_row)
选项2
keep_data = [arow in row_list if arow] # Or w/e logic. This will be faster.
UICData.append(keep_data)
row_list
包含一组值,例如:
[1.01, 75, 3.56, ...]
当您调用
for each_row in row_list:
时,您将为循环的每次迭代分配一个float 值给
each_row
。然后你尝试这样做:
if not all(map(len, each_row)):
Python 的
map
函数需要一个列表作为第二个参数,并尝试迭代它以将函数
len
应用于列表中的每个项目。您无法迭代浮点数。我不完全确定你想在这里做什么,但如果你想检查
row_list
中的所有项目都不是
None
或空字符串,那么你可以这样做:
if None not in row_list and '' not in row_list:
UICData.append(row_list)
正如您所见,使用
map()
不是一个好主意;仅当所有字段均为文本时才适用。您应该通常根据数据类型,特别是单个列来应用测试。验证行中的项目后,您就可以输出数据了。您选择的路径 (1) 构建所有输出行的列表 (2) 使用 xlwt 写入临时 XLS 文件 (3) 使用
xlrd
读取临时文件并使用
unicodecsv
写入 CSV 文件。请考虑避免这一切;相反,只需使用
unicodecsv.writer.writerow(row_list)