1

[更新 - 2020 年 5 月 15 日 - 我得到了这段代码,整个流程都使用镶木地板文件格式。但是,我仍然对使用 CSV 的方法感兴趣]

我正在尝试使用以下命令将 csv 文件从本地计算机上传到 ADLS Gen 2 存储。这工作正常,但 ADLS 中生成的 csv 文件是连续文本,没有用于分隔每一行的换行符。无法像使用 Polybase 一样将此 CSV 文件加载到 Azure Synapse。

输入 CSV -

"col1","col2","col3"

“新泽西州”、“1”、“2020 年 1 月 3 日”

“纽约”、“1”、“2020 年 1 月 4 日” ...

我得到的输出 CSV 是这样的 -

"col1","col2","col3""NJ","1","1/3/2020""NY","1","1/4/2020"...

如何确保我的最终 csv 在每一行之后都有换行符?每个 CSV 中只有 100,000 条记录。

import os, uuid, sys
from azure.storage.filedatalake import DataLakeServiceClient
from azure.core._match_conditions import MatchConditions
from azure.storage.filedatalake._models import ContentSettings

try:  
    global service_client
    service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
        "https", "<storage-account>"), credential="<secret>")

    file_system_client = service_client.get_file_system_client(file_system="import")
    dest_directory_client = file_system_client.get_directory_client("Destination")

    f = open("file-path/cashreceipts.csv",'r')

    dest_file_client = dest_directory_client.create_file("cashreceipts.csv")

    file_contents = f.read()

    dest_file_client.upload_data(file_contents, overwrite=True)
    f.close()

except Exception as e:
    print(e)

我也尝试过这种方法 -

dest_file_client.append_data(data=file_contents, offset=0, length=len(file_contents))
dest_file_client.flush_data(len(file_contents))

我指的是这里的 Microsoft 文档,它描述了文本文件的方法 - https://docs.microsoft.com/en-us/azure/storage/blobs/data-lake-storage-directory-file-acl-python

4

0 回答 0