【问题标题】:OSError: [Errno 36] File name too long: for python package and .txt file, pandas openingOSError: [Errno 36] File name too long: for python package and .txt file, pandas opening
【发布时间】:2021-09-22 22:01:53
【问题描述】:

错误 OSError: [Errno 36] 文件名太长: 以下代码:

from importlib_resources import open_text
import pandas as pd

with open_text('package.data', 'librebook.txt') as f:
    input_file = f.read()

dataset = pd.read_csv(input_file)

Ubuntu 20.04 OS,这是一个python包,init.py文件

我不想使用 .readlines()

我能否以不同的方式构建此代码以防止出现此结果?我需要修改我的操作系统吗?我发现的一些帮助看起来是为了修改操作系统,但如果我不需要,我不想这样做。谢谢。

【问题讨论】:

    标签: python python-3.x pandas dataframe ubuntu


    【解决方案1】:

    为什么不只传入文件名而不是内容

    dataset = pd.read_csv('librebook.txt')
    

    【讨论】:

    • 谢谢,我不相信这将适用于 python 包结构和文件导入。在我将代码移植到包中之前,我的结构是这样的。
    • 我不知道为什么它不起作用,但如果你想要另一个将 csv 读入 pandas 的例子,这里是 stack overflow 。 stackoverflow.com/questions/26903304/… 。读取 csv int o pandas 很常见。
    • 包的结构我做不到 dataset = pd.read_csv('librebook.txt') 包不知道去哪里找文件
    【解决方案2】:
    from importlib_resources import path
    import pandas as pd
    
    with path('package.data', 'librebook.txt') as f:
        dataset = pd.read_csv(f)
    

    【讨论】:

      猜你喜欢
      • 2020-07-18
      • 2019-03-25
      • 1970-01-01
      • 2020-02-09
      • 2015-10-20
      • 1970-01-01
      • 2016-06-07
      • 2017-07-05
      • 2018-11-20
      相关资源
      最近更新 更多