【问题标题】:How to save .csv file to a directory with a new file name如何将 .csv 文件保存到具有新文件名的目录
【发布时间】:2020-01-07 19:00:15
【问题描述】:

我在 Jupiter Lab 使用笔记本电脑工作。我需要使用 pandas/python 代码将 .csv 文件以新名称保存到我的计算机的特定目录下。

问题如下: 将数据保存在“工作”目录中并将其命名为“数据问题 1”

我已经尝试了许多代码,但这似乎应该可以工作。

import os.path

save_path = '/Users/ccheddar/Class700/Homework/Data/Working'

name_of_file = raw_input("Cityline_Calls_for_Service.csv")

completeName = os.path.join(save_path, name_of_file) 

file1 = open(completeName, "w")

toFile = raw_input("syr_service.csv")

file1.write(toFile)

file1.close()

我不断收到错误消息:

NameError                                 Traceback (most recent call last)
<ipython-input-40-f34c54969f7c> in <module>
      3 save_path = '/Users/ccheddar/Class700/Homework/Data/Working'
      4 
----> 5 name_of_file = raw_input("Cityline_Calls_for_Service.csv")
      6 
      7 completeName = os.path.join(save_path, name_of_file)

NameError: name 'raw_input' is not defined

【问题讨论】:

  • 你试过什么?尝试给出一个您尝试过的代码示例以及为什么它不起作用。
  • 您正在使用 Python 3(您应该正在使用)但您的代码是为 Python 2 编写的。raw_input 在 Python 3 中不存在,它只是只需input
  • 虽然实际上,不知道你为什么会使用input/raw_input,那是为了接受用户输入。看起来你只想要那些字符串......
  • 感谢您的帮助!这段代码最终完成了我需要做的事情:export_csv = df.to_csv (r'C:\Users\Ron\Desktop\export_dataframe.csv', index = None, header=True) #Don't forget to add '. csv'在路径的末尾

标签: python pandas directory jupyter-notebook save


【解决方案1】:

只需删除raw_input

import os.path

save_path = '/Users/ccheddar/Class700/Homework/Data/Working'

name_of_file = "Cityline_Calls_for_Service.csv"

completeName = os.path.join(save_path, name_of_file) 

file1 = open(completeName, "w")

toFile = "syr_service.csv"

file1.write(toFile)

file1.close()

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 2021-07-04
    • 2012-11-04
    • 2017-07-03
    相关资源
    最近更新 更多