我的数据如下所示,它位于扩展名为 xlsx 的 excel 文件中,因此我使用openpyxl
库在 python 中读取文件并从第 6 行开始提取 2 列的数据,并将提取的数据附加到带有键的字典中“Column_name”和“Column_Type”。这样做的原因是,我想在提取的数据之间添加额外的字符串。我要添加的字符串是 CREATE TABLE TRANSIENT TABLE STG_EMPLOYEE();
到目前为止,我的代码和输出如下所示。我在 txt 文件中的预期输出也在下面提到。如果您注意到,我想同时从两个键中枚举值,我不确定如何实现这一点,python
所以感谢您的帮助?
提前感谢您的时间和精力!
数据
File Name: Employee
Sheet Name: Employee
File Type: csv
Field Name Type
Name String
Salary Numeric
Date Date
Phone Int
到目前为止的代码
from openpyxl import load_workbook
data_file='\\test.xlsx'
# Load the entire workbook.
wb = load_workbook(data_file)
ws = wb['Employee'] #Access Sheet
outputFile = open('/output.txt', 'w') # Text file Output
outputFile.write("CREATE TABLE TRANSIENT TABLE STG_EMPLOYEE({});".format(theString) + "\n")
mylines={"Column_name":[],"Column_Type":[]} #Getting 2 columns data from row 6
for i in range(6, ws.max_row+1):
name = ws.cell(row=i, column=1).value
name1=ws.cell(row=i, column=2).value
mylines["Column_name"].append(name) #Appending dictionary key "Column_name"
mylines["Column_Type"].append(name1) #Appending dictionay key "Column_type"
theString = " "
# This relies on the lists 'Column_name' and 'Column_Type' always being the same length
# I.e., there should always be a value for each key
for i in range(len(mylines['Column_name'])):
theString += mylines['Column_name'][i] + " " + mylines['Column_Type'][i]
if i < ws.max_row:
theString += ", "
outputFile.close()
使用上面的代码在 txt 文件中输出:
CREATE TABLE TRANSIENT TABLE STG_EMPLOYEE([['Name', 'Salary', 'Date', 'Phone'], ['String', 'Numeric', 'Date', 'Int']]);
Txt 文件中的预期输出
CREATE TABLE TRANSIENT TABLE STG_EMPLOYEE( Name String, Salary Numeric, Date Date, Phone Int);
您可以利用"Column_name"
和"Column_Type"
列表中应始终存在相同数量的项目这一事实,以使用单个变量将问题减少到迭代:
# Simulated Excel Data
ws = {
'max_row': 3,
'cell': [
['Name', 'String'],
['Salary', 'Numeric'],
['Date', 'Date'],
['Phone', 'Int']
]
}
mylines={"Column_name":[],"Column_Type":[]}
for i in range(0, ws['max_row']+1):
name = ws['cell'][i][0]
name1=ws['cell'][i][1]
mylines["Column_name"].append(name)
mylines["Column_Type"].append(name1)
theString = " "
# This relies on the lists 'Column_name' and 'Column_Type' always being the same length
# I.e., there should always be a value for each key
for i in range(len(mylines['Column_name'])):
theString += mylines['Column_name'][i] + " " + mylines['Column_Type'][i]
if i < ws['max_row']:
theString += ", "
# OLD
print("CREATE TABLE TRANSIENT TABLE STG_EMPLOYEE({});".format([(mylines[k]) for k,v in mylines.items()]) + "\n")
# NEW
print("CREATE TABLE TRANSIENT TABLE STG_EMPLOYEE({});".format(theString) + "\n")
当我运行它时,我得到以下输出(旧版本的第一行和新版本的第二行):
CREATE TABLE TRANSIENT TABLE STG_EMPLOYEE([['Name', 'Salary', 'Date', 'Phone'], ['String', 'Numeric', 'Date', 'Int']]);
CREATE TABLE TRANSIENT TABLE STG_EMPLOYEE( Name String, Salary Numeric, Date Date, Phone Int);
当然,如果您的电子表格中有实际的数字和其他非字符串数据,您还需要在将变量值str()
附加到theString
.
与您的代码的唯一区别(除了我为使代码在没有实际电子表格的情况下作为独立代码工作所做的更改)是将format
最后一行中的参数替换为通过将两个列表中的值附加到mylines
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句