Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将Python变量用作CSV文件的文件名_Python_Python 3.x_Pandas_Csv - Fatal编程技术网

无法将Python变量用作CSV文件的文件名

无法将Python变量用作CSV文件的文件名,python,python-3.x,pandas,csv,Python,Python 3.x,Pandas,Csv,我想使用Pandas dataframe中的时间戳图形作为CSV输出的文件名,但遇到了一条错误消息 以下是程序 以下是源CSV文件的链接: 读取CSV文件,并创建一个名为“df”的数据帧 从数据帧中提取时间戳的图形,并将它们合并为“df_time” 然后,我尝试使用“df_time”作为CSV输出的文件名 在此之后,我遇到了以下错误 OSError Traceback (most recent call last) <ip

我想使用Pandas dataframe中的时间戳图形作为CSV输出的文件名,但遇到了一条错误消息

以下是程序

以下是源CSV文件的链接:

  • 读取CSV文件,并创建一个名为“df”的数据帧
  • 从数据帧中提取时间戳的图形,并将它们合并为“df_time”
  • 然后,我尝试使用“df_time”作为CSV输出的文件名
  • 在此之后,我遇到了以下错误

    OSError                                   Traceback (most recent call last)
    <ipython-input-169-09dbe0d815ec> in <module>
    ----> 1 df.to_csv('test_' + df_time + '.csv')
    
    ~\anaconda3\lib\site-packages\pandas\core\generic.py in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, decimal)
       3202             decimal=decimal,
       3203         )
    -> 3204         formatter.save()
       3205 
       3206         if path_or_buf is None:
    
    ~\anaconda3\lib\site-packages\pandas\io\formats\csvs.py in save(self)
        186                 self.mode,
        187                 encoding=self.encoding,
    --> 188                 compression=dict(self.compression_args, method=self.compression),
        189             )
        190             close = True
    
    ~\anaconda3\lib\site-packages\pandas\io\common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text)
        426         if encoding:
        427             # Encoding
    --> 428             f = open(path_or_buf, mode, encoding=encoding, newline="")
        429         elif is_text:
        430             # No explicit encoding
    
    OSError: [Errno 22] Invalid argument: 'test_2020/11/10 7:00:00-2020/11/9 7:30:00.csv'
    
    OSError回溯(最近一次调用)
    在里面
    ---->1 df.至_csv('test_'+df_time+'.csv'))
    ~\anaconda3\lib\site packages\pandas\core\generic.py in to_csv(self、path或buf、sep、na_rep、float_格式、列、标题、索引、索引标签、模式、编码、压缩、引号、引号、行终止符、chunksize、日期_格式、双引号、转义、十进制)
    3202十进制=十进制,
    3203         )
    ->3204格式化程序。保存()
    3205
    3206如果路径_或_buf为无:
    保存中的~\anaconda3\lib\site packages\pandas\io\formats\csvs.py(self)
    186自我模式,
    187编码=自编码,
    -->188压缩=dict(自压缩参数,方法=自压缩),
    189             )
    190关闭=正确
    get\u句柄中的~\anaconda3\lib\site packages\pandas\io\common.py(路径、模式、编码、压缩、内存映射、是文本)
    426如果编码:
    427编码
    -->428 f=打开(路径\或\ buf,模式,编码=编码,换行=“”)
    429 elif为文本:
    430#无显式编码
    OSError:[Errno 22]无效参数:“test_2020/11/10 7:00:00-2020/11/9 7:30:00.csv”
    
    我使用Python3.7.6,并找到了一些解决方案来解决“[Errno 22]Invalid argument”。然而,我在这种情况下找不到类似的案例


    如果您能提供避免此错误的建议,我将不胜感激。

    您无法使用某些字符(如“/”)保存文件,请以可读的方式调整时间戳变量
    df_time
    ,然后将其保存为csv。

    您能尝试在文件命名时不使用字符/和:?非常感谢。我删除了/和:,并解决了问题。我从变量中删除了:和/后,问题就解决了。非常感谢你。
    df.to_csv('test_' + df_time + '.csv')
    
    OSError                                   Traceback (most recent call last)
    <ipython-input-169-09dbe0d815ec> in <module>
    ----> 1 df.to_csv('test_' + df_time + '.csv')
    
    ~\anaconda3\lib\site-packages\pandas\core\generic.py in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, decimal)
       3202             decimal=decimal,
       3203         )
    -> 3204         formatter.save()
       3205 
       3206         if path_or_buf is None:
    
    ~\anaconda3\lib\site-packages\pandas\io\formats\csvs.py in save(self)
        186                 self.mode,
        187                 encoding=self.encoding,
    --> 188                 compression=dict(self.compression_args, method=self.compression),
        189             )
        190             close = True
    
    ~\anaconda3\lib\site-packages\pandas\io\common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text)
        426         if encoding:
        427             # Encoding
    --> 428             f = open(path_or_buf, mode, encoding=encoding, newline="")
        429         elif is_text:
        430             # No explicit encoding
    
    OSError: [Errno 22] Invalid argument: 'test_2020/11/10 7:00:00-2020/11/9 7:30:00.csv'