如何枚举两个键的字典值并使用Python将值输出到txt文件中?

How to enumarte through dictionary values from two keys and output the values in txt file using Python?

我的数据如下所示,它位于扩展名为 xlsx 的 excel 文件中,因此我使用 openpyxl 库读取 python 中的文件并从中提取数据第 6 行开始为 2 列,我将提取的数据附加到带有键“Column_name”和“Column_Type”的字典中。这样做的原因是,我想在提取的数据之间添加额外的字符串。我要添加的字符串是 CREATE TABLE TRANSIENT TABLE STG_EMPLOYEE();

到目前为止,我的代码和输出看起来像 below.My 下面还提到了 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);

当然,如果您的电子表格中有实际的数字和其他 non-string 数据,您还需要使用 str() 将变量值转换为字符串,然后再将它们附加到 [=15] =].

与您的代码的唯一区别(除了我为使代码在没有实际电子表格的情况下独立运行所做的更改之外)是将最后一行中的 format 参数替换为通过附加构建的字符串mylines.

中两个列表的值