如何使用自定义活动将 .dbf 文件转换为 .csv 文件?任何完整的答案都会对我非常有帮助,因为我是新手

问题描述 投票:0回答:1

如何使用自定义活动将 .dbf 文件转换为 .csv 文件?任何完整的答案都会对我非常有帮助。

我尝试重命名文件并查看数据,但我们得到的数据是非结构化格式。我想加载我的 ssms 表中的数据。

python azure azure-data-factory
1个回答
0
投票

要将

.dbf
文件转换为
.csv
文件,您可以尝试使用以下代码。它使用示例
marks.dbf
文件并转换为
.csv
文件,如下面的输出所示。

def dbfToCsv(dbfFile, csvFile):
    try:
        if not os.path.exists(dbfFile):
            print("Error: The specified .dbf file does not exist.")
            return
        
        newTable = DBF(dbfFile)
        
        with open(csvFile, 'w', newline='') as newcsvfile:
            writer = csv.writer(newcsvfile)
            
            writer.writerow(newTable.field_names)
            
            for record in newTable:
                writer.writerow(list(record.values()))
        
        print("Conversion completed successfully.")
    
    except Exception as e:
        print(f"An error occurred: {e}")

dbfFilePath = r"C:\Users\****\****\marks.dbf"
csvFilePath = r"C:\Users\****\****\Desktop\MyTrails\output2.csv"

dbfToCsv(dbfFilePath, csvFilePath)

以下是示例

.dbf
文件: enter image description here

运行上述代码后,它成功地将

.dbf
转换为
.csv
,如下面的输出所示。

输出:

Conversion completed successfully.
REG_NO,NAME,MARKS
1000,Aaradhya,86
1001,Adah,91
1002,Adhira,72
1003,Alisha,69
1004,Amoli,88

.py
文件中使用此代码并在自定义活动中执行此代码。您可以浏览此文档以了解有关自定义活动的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.