Python imp - Access the import internals - imp.load_source(name, pathname[, file])

Python imp - Access the import internals - imp.load_source{name, pathname[, file]}

imp - Access the import internals
https://docs.python.org/2.7/library/imp.html

This module provides an interface to the mechanisms used to implement the import statement.
此模块提供了用于实现 import 语句的机制的接口。

mechanism ['mek(ə)nɪz(ə)m]:n. 机制,原理,途径,进程,机械装置,技巧
internal [ɪn'tɜːn(ə)l]:n. 内脏,本质 adj. 内部的,里面的,体内的,内部的

1. imp.load_source(name, pathname[, file])

Load and initialize a module implemented as a Python source file and return its module object. If the module was already initialized, it will be initialized again. The name argument is used to create or access a module object. The pathname argument points to the source file. The file argument is the source file, open for reading as text, from the beginning. It must currently be a real file object, not a user-defined class emulating a file. Note that if a properly matching byte-compiled file (with suffix .pyc or .pyo) exists, it will be used instead of parsing the given source file.
加载并初始化实现为 Python 源文件的模块并返回其模块对象。如果模块已经初始化,它将再次初始化。name 参数用于创建或访问模块对象。pathname 参数指向源文件。file 参数是源文件,从头开始打开以作为文本读取。它当前必须是真实的文件对象,而不是模拟文件的用户定义的类。请注意,如果存在正确匹配的字节编译文件 (带后缀 .pyc.pyo),则将使用它而不是解析给定的源文件。

suffix ['sʌfɪks]:n. 后缀,词尾 v. 添后缀

2. sys.path.append()

forever.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function

def print_function(first_name, last_name):
    print(first_name)
    print(last_name)

yongqiang.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function

import sys

sys.path.append("/home/strong/git_workspace/MonoGRNet/strong/")
import forever

forever.print_function("yongqiang", "cheng")
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py 
yongqiang
cheng
strong@foreverstrong:~/git_workspace/MonoGRNet$

3. imp.load_source

forever.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function

def print_function(first_name, last_name):
    print(first_name)
    print(last_name)

yongqiang.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function

import imp

imp.load_source("tian", "/home/strong/git_workspace/MonoGRNet/strong/forever.py")
import tian

tian.print_function("yongqiang", "cheng")
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py 
yongqiang
cheng
strong@foreverstrong:~/git_workspace/MonoGRNet$ 

4. imp.load_source

forever.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function

def print_function(first_name, last_name):
    print(first_name)
    print(last_name)

yongqiang.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function

import imp

model = imp.load_source("tian", "/home/strong/git_workspace/MonoGRNet/strong/forever.py")
model.print_function("yongqiang", "cheng")
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py 
yongqiang
cheng
strong@foreverstrong:~/git_workspace/MonoGRNet$ 

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

这个错误是因为你的表头 `header` 和原始数据的列数不匹配导致的。根据错误信息,预期的轴(axis)只有一个元素,而新值有五个元素。 如果你的原始数据中已经有了一些列,你可以尝试使用以下代码来动态添加表头: ```python import os import pandas as pd output_dir = 'output' header = ['Year', 'Mon', 'Day', 'PRE'] for file in os.listdir(output_dir): if file.endswith('.xlsx'): file_path = os.path.join(output_dir, file) df = pd.read_excel(file_path) existing_columns = list(df.columns) new_columns = header + existing_columns df = df.reindex(columns=new_columns) df.to_excel(file_path, index=False) ``` 上述代码中,我们首先读取每个 Excel 文件,获取原始数据的列名。然后,我们将新的表头与原始列名合并为一个新的列表 `new_columns`。接下来,我们使用 `reindex()` 方法重新排列数据框的列,将新增的表头添加到数据框中。最后,我们将更新后的数据框重新保存到原始的 Excel 文件中。 如果你的原始数据没有列名,你可以尝试使用以下代码将表头添加到空数据框中: ```python import os import pandas as pd output_dir = 'output' header = ['Year', 'Mon', 'Day', 'PRE'] for file in os.listdir(output_dir): if file.endswith('.xlsx'): file_path = os.path.join(output_dir, file) df = pd.DataFrame(columns=header) df.to_excel(file_path, index=False) ``` 上述代码中,我们创建一个空的数据框,并指定列名为 `header`。然后,我们将这个数据框保存为一个新的 Excel 文件,覆盖原始的文件。这样就可以将表头添加到原始数据中了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值