pandas 如何使用 Python 直接从服务器读取 excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45740273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:16:29  来源:igfitidea点击:

How to read an excel file directly from a Server with Python

pythonpandasos.path

提问by DGMS89

Scenario:I am trying to read a excel file from a server folder and after that read each worksheet of that file into a dataframe and perform some operations.

场景:我试图从服务器文件夹中读取一个 excel 文件,然后将该文件的每个工作表读入一个数据帧并执行一些操作。

Issue:I have trying multiple approaches but facing different situations: either I read the file, but it is seen as a str and the operations cannot be performed, or the file is not read.

问题:我尝试了多种方法,但面临不同的情况:我读取了文件,但将其视为 str 并且无法执行操作,或者未读取文件。

What I tried so far:

到目前为止我尝试过的:

#first attempt
os.path(r'\X\str\Db\C\Source\selection\Date\Test','r')  

#second attempt
directory = os.getcwd() + "\C\Source\selection\Date\Test"

#third attempt
f = os.getcwd() + "\C\Source\selection\Date\Test\12.xlsx"

#fourth attempt
f = open(r'\X\str\Db\C\Source\selection\Date\Test.xlsx', 'r')

db1 = pd.DataFrame()
db2 = pd.DataFrame()
db3 = pd.DataFrame()
bte = pd.DataFrame()
fnl = pd.DataFrame()

wb = load_workbook(f)

for sheet in wb.worksheets:

    if sheet.title == "db1":

        db1 = pd.read_excel(f, "db1")

Obs:I also researched the documentation for reading with pd and some other similar questions in SO, but still could not solve this problem. Ex: Python - how to read path file/folder from serverUsing Python, how can I access a shared folder on windows network?https://docs.python.org/release/2.5.2/tut/node9.html#SECTION009200000000000000000

Obs:我还研究了用 pd 阅读的文档和 SO 中的其他一些类似问题,但仍然无法解决这个问题。例如: Python - 如何从服务器读取路径文件/文件夹使用 Python,我如何访问 Windows 网络上的共享文件夹?https://docs.python.org/release/2.5.2/tut/node9.html#SECTION009200000000000000000

Question:What is the proper way to achieve this?

问题:实现这一目标的正确方法是什么?

回答by Joao Vitorino

You need to open the file as rb mode

您需要以 rb 模式打开文件

b = bynary file r = only read the file

b = 二进制文件 r = 只读取文件

f = open('//X/str/Db/C/Source/selection/Date/Test/12.xlsx', 'rb')

You can use pandas librarythat will do most of the work for you

您可以使用可以为您完成大部分工作的Pandas 库

import pandas

进口大Pandas

import pandas
f = pandas.read_excel(open('//X/str/Db/C/Source/selection/Date/Test/12.xlsx','rb'), sheetname='Sheet 1')
# or using sheet index starting 0
f = pandas.read_excel(open('//X/str/Db/C/Source/selection/Date/Test/12.xlsx','rb'), sheetname=2)

There is a similar question here

还有一个类似的问题在这里

回答by Okroshiashvili

I had same issue. Try Pandas and forward slashes

我有同样的问题。尝试 Pandas 和正斜杠

pd.read_excel('//X/str/Db/C/Source/selection/Date/Test/12.xlsx') 

Have to work perfectly

必须完美地工作

回答by Zac Crites

From here.

从这里。

Try using forward slashes in your UNC path:

尝试在 UNC 路径中使用正斜杠:

f = open('//X/str/Db/C/Source/selection/Date/Test/12.xlsx', 'rb')