首页 > 解决方案 > Python 将 CSV 上传到 Sharepoint Online:上传后格式不正确

问题描述

使用 python,我将一个 csv 文件在线上传到 Sharepoint。我能够将文件添加到 Sharepoint 站点文档列表,但是当我打开 CSV 文件时,它的格式不正确。

Example: 

Source file: 
"FirstName", "LastName", "Date"
"John", "Smith", "08-09-1990"
"Jane", "doe", "01-02-2010"

Destination Sharepoint file: (after upload to Sharepoint online) 
"FirstName"\ "LastName"\ "Date"\ "John"\ "Smith"\ "08-09-1990"\ "Jane"\ "doe"\ "01-02-2010"

目标 Sharepoint 文件最终具有修改的分隔符,并且所有数据都被解析为单行。我相信单行解析是由于从“,”分隔符更改为“”分隔符。

如何使用行/列保持源文件格式?下面是我正在使用的代码。

#Connect to Sharepoint
ctx_auth = AuthenticationContext(url_shrpt)
if ctx_auth.acquire_token_for_user(username_shrpt, password_shrpt):
  ctx = ClientContext(url_shrpt, ctx_auth)
  web = ctx.web
  ctx.load(web)
  ctx.execute_query()
  print('Authenticated into sharepoint as: ',web.properties['Title'])

else:
  print(ctx_auth.get_last_error())
##### ---- ####  

#Upload the files
try:
        #list all of the files in the folder
    
    
        for file in fileNames:
            #Open individual files and read content
            readFile = open(file_name + file, 'r')
            data = readFile.read()

            #Set the destination path for the files to be sent
            remotepath = r'Shared Documents/Folder/SubFolder/'+file
    
            #List the director, then split the files to get the file name
            dir, name = os.path.split(file)
            file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(remotepath, data).execute_query()
            print("File has been uploaded to Sharepoint")

            #close the file so it can be archived
            readFile.close()
    
    
except Exception as err:
        print("Something went wrong with writing files: " + str(err))

标签: pythoncsvsharepointformatting

解决方案


推荐阅读