Python:__main__ 上的属性查找 read_csvfiles 失败

发布于 2025-01-17 03:43:04 字数 1918 浏览 2 评论 0原文

我有一个脚本可以读取大量 csv 文件并将它们放入 pandas 数据框中进行进一步处理。该脚本包含这两个函数,其中第二个函数调用第一个函数:

def read_csvfiles(date, folder_path, icon, info, file_suffix):
    d1 = pd.to_datetime(date)     
    if icon == 'icon':
        subfolder = '/'
    else:
        subfolder = '/G' + icon + str(d1.year)  
    df = pd.DataFrame(data=None)                                  
    for s in info:
        datestring = d1.strftime('%m-%d')
        file_path = folder_path + subfolder + str(d1.year) + f'-{datestring}-00/{s}' + file_suffix
        try:
            temp_df = pd.read_csv(file_path, sep=';', header=None, decimal='.', skiprows=1, index_col=0)
        except FileNotFoundError:
            print("File not found: " + subfolder + f'-{datestring}-00/{s}{file_suffix}, values were set to NaN')
            temp_df = pd.DataFrame(nan, columns=[s], index=pd.date_range(start=d1, periods=24 * 5 + 1, freq='H'))
        df[s] = temp_df.iloc[:, 0]
    if df.index.dtype != dtype('datetime64[ns]'):
        df.index = map(lambda num: datetime.datetime.strptime(str(num), '%d.%m.%Y %H:%M:%S'), df.index)
    return date, df


def read_fc(folder_path: str, icon: str, date_start: datetime.datetime, date_end: datetime.datetime, info: list, file_suffix: str) -> pd.Series:
    dates = pd.date_range(date_start, date_end, freq='d')
    pool = Pool()
    readfun = partial(read_csvfiles,
                      folder_path=folder_path,
                      icon=icon,
                      info=info,
                      file_suffix=file_suffix)
    data = pool.map(readfun, dates)
    return pd.Series(dict(data))

在上次 Windows 更新后,在 if __name__ == '__main__': 之后使用第二个函数给我一个错误,但没有之前发生过,无需更改脚本中的任何内容:

“_pickle.PicklingError:无法pickle:属性查找read_csvfiles on ma​​in failed”

有谁知道为什么现在会发生此错误以及我可以采取哪些措施来修复它?

I have a script that reads a lot of csv files and puts them into a pandas dataframe for further processing. This script contains these two functions, where the second one calls the first one:

def read_csvfiles(date, folder_path, icon, info, file_suffix):
    d1 = pd.to_datetime(date)     
    if icon == 'icon':
        subfolder = '/'
    else:
        subfolder = '/G' + icon + str(d1.year)  
    df = pd.DataFrame(data=None)                                  
    for s in info:
        datestring = d1.strftime('%m-%d')
        file_path = folder_path + subfolder + str(d1.year) + f'-{datestring}-00/{s}' + file_suffix
        try:
            temp_df = pd.read_csv(file_path, sep=';', header=None, decimal='.', skiprows=1, index_col=0)
        except FileNotFoundError:
            print("File not found: " + subfolder + f'-{datestring}-00/{s}{file_suffix}, values were set to NaN')
            temp_df = pd.DataFrame(nan, columns=[s], index=pd.date_range(start=d1, periods=24 * 5 + 1, freq='H'))
        df[s] = temp_df.iloc[:, 0]
    if df.index.dtype != dtype('datetime64[ns]'):
        df.index = map(lambda num: datetime.datetime.strptime(str(num), '%d.%m.%Y %H:%M:%S'), df.index)
    return date, df


def read_fc(folder_path: str, icon: str, date_start: datetime.datetime, date_end: datetime.datetime, info: list, file_suffix: str) -> pd.Series:
    dates = pd.date_range(date_start, date_end, freq='d')
    pool = Pool()
    readfun = partial(read_csvfiles,
                      folder_path=folder_path,
                      icon=icon,
                      info=info,
                      file_suffix=file_suffix)
    data = pool.map(readfun, dates)
    return pd.Series(dict(data))

After the last windows update, using the second function after if __name__ == '__main__': gives me an error that didn't occure before, without changing anything in the script:

"_pickle.PicklingError: Can't pickle <function read_csvfiles at 0x0000024FB2C53488>: attribute lookup read_csvfiles on main failed"

Does anybody have any idea, why this error is occuring now and what I can do to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文