python - 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))
解决方案
推荐阅读
- android - 用于预提交的 Sherter Gradle 插件
- python - 更改矩阵中的数据类型
- php - 从已使用复选框发布的数据中插入数据
- javascript - jQuery 验证器在 ajax 调用后验证不再存在的表单元素
- java - 在 Oozie 上的 java 操作中运行 shell 命令会引发权限被拒绝
- access-token - 在 IIS 上本地托管 Azure 应用服务后端时,“此请求的授权已被拒绝”
- javascript - JavaScript 和 HTML 元素操作
- react-native - 使用 React Navigation 的 React Native 组件中未定义的未声明容器
- node.js - 'electron-packager' 不是内部或外部命令、可运行程序或批处理文件
- sql - SQL 多列具有一列作为关系