如何使用 Python 直接从服务器读取 excel 文件
How to read an excel file directly from a Server with Python
场景: 我正在尝试从服务器文件夹中读取 excel 文件,然后将该文件的每个工作表读取到数据框中并执行一些操作。
问题:我尝试了多种方法但面临不同的情况:要么我读取了文件,但它被视为一个 str 并且无法执行操作,要么文件未读。
到目前为止我尝试了什么:
#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: 我也研究了for reading with pd的文档和SO中的其他一些类似问题,但仍然无法解决这个问题。前任:
Python - how to read path file/folder from server
Using Python, how can I access a shared folder on windows network?
https://docs.python.org/release/2.5.2/tut/node9.html#SECTION009200000000000000000
问题:实现这个的正确方法是什么?
From here.
尝试在您的 UNC 路径中使用正斜杠:
f = open('//X/str/Db/C/Source/selection/Date/Test/12.xlsx', 'rb')
您需要以 rb 模式打开文件
b = 二进制文件
r = 只读取文件
f = open('//X/str/Db/C/Source/selection/Date/Test/12.xlsx', 'rb')
您可以使用 pandas library 来完成大部分工作
进口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)
有个类似的问题here
我有同样的问题。试试 Pandas 和正斜杠
pd.read_excel('//X/str/Db/C/Source/selection/Date/Test/12.xlsx')
必须完美工作
场景: 我正在尝试从服务器文件夹中读取 excel 文件,然后将该文件的每个工作表读取到数据框中并执行一些操作。
问题:我尝试了多种方法但面临不同的情况:要么我读取了文件,但它被视为一个 str 并且无法执行操作,要么文件未读。
到目前为止我尝试了什么:
#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: 我也研究了for reading with pd的文档和SO中的其他一些类似问题,但仍然无法解决这个问题。前任: Python - how to read path file/folder from server Using Python, how can I access a shared folder on windows network? https://docs.python.org/release/2.5.2/tut/node9.html#SECTION009200000000000000000
问题:实现这个的正确方法是什么?
From here.
尝试在您的 UNC 路径中使用正斜杠:
f = open('//X/str/Db/C/Source/selection/Date/Test/12.xlsx', 'rb')
您需要以 rb 模式打开文件
b = 二进制文件 r = 只读取文件
f = open('//X/str/Db/C/Source/selection/Date/Test/12.xlsx', 'rb')
您可以使用 pandas library 来完成大部分工作
进口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)
有个类似的问题here
我有同样的问题。试试 Pandas 和正斜杠
pd.read_excel('//X/str/Db/C/Source/selection/Date/Test/12.xlsx')
必须完美工作