Python/Pandas:正在写入网络驱动器上的文件(Windows)

hk8txs48  于 2023-01-28  发布在  Python
关注(0)|答案(2)|浏览(170)

我正在尝试写入网络驱动器上的文件。我没有收到任何错误,但无法创建该文件。有什么建议吗?

# Dataframe
df = pd.DataFrame(rwb_list_departments,columns=['Owned By Team','Major Incident','Status','Priority','Incident ID','Owned By','Short Description','Requester Display Name','Created Date Time','Closed Date Time','Department','Duration','NumDur'])

# writing to local drive as csv works great
df.to_csv('rwb_itsm_processed_tickets.csv',index=False,encoding='utf-8')

# Now write file to networked drive as excel
data_folder = Path('W:/CDS Analytics/RWB')

# combine path and file
file_to_write = data_folder / 'rwb_itsm_processed_tickets.xlsx'

# Specify a writer
writer = pd.ExcelWriter('file_to_write', engine='xlsxwriter')

# Write your DataFrame to a file     
df.to_excel(writer,'report')

# Save the result
writer.save()
mrwjdhj3

mrwjdhj31#

这就是我如何让它在我的Mac上工作的

# Now write file to networked drive as excel
data_folder ='/Users/myname/Desktop/'

# combine path and file
file_to_write = data_folder + '/rwb_itsm_processed_tickets.xlsx'

附言:我无法访问rwb_list_departments,所以我只是采取了以下示例 Dataframe df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8]], columns=["A", "B"])和文件一起创建的 Dataframe 数据。其余的一切都保持相同,在您的代码。错误似乎是在命令Path,然后你的方式定义file_to_write

5t7ly7z5

5t7ly7z52#

import os

user = os.environ.get('USERNAME') 

Data_Folder = r'C:\\Users\\'+ user +'\\Desktop\\'

file = data_folder + 'rwb_itsm_processed_tickets.xlsx'

我不知道为什么,但我的双“\”没有显示在我的答复,但使用双反斜杠,而不是单\

相关问题