注意,是windows版本!
1.发布函数模块
step1:创建一个自己的函数,保存名为nesert.py。
如下:
"""此函数用来实现嵌套数组的分层输出"""
"""包含三个参数:数组、是否打开缩进值的布尔类型数据、控制缩进从哪里开始、文件保存的位置"""
import sys
def print_lol(the_list, indent=False, level=0, fh=sys.stdout):
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item, indent, level+1, fh)
else:
if indent:
print("\t" * level, end='', file=fh)
print(each_item, file=fh)
step2:创建一个setup.py文件。
具体内容如下:
from setuptools import setup
setup(
name = 'nester',
version = '1.0.0',
py_modules = ['nester'],
author = 'wyk'
)
"""中间是setup的参数,可以搜索具体内容包括什么,其中前三个是必要的参数"""
step3:创建一个文件夹(命名为nester),将两个文件都放进去:

step4:在该文件夹中右击选择在终端中打开,进入dos命令行。
首先输入:
python setup.py sdist
输出行显示:

文件夹出现:

再在dos中输入:
python setup.py install
输出行显示:
文件夹出现:

此时已经打包成模块了,具体模块在dist中。
step4:接下来我们验证一下:
在python的shell中输入:
import sys
print(sys.path)
将输出包含'C:\\Users\\01\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\nester-1.0.0-py3.12.egg'的路径(具体路径不重要,主要是包含函数名,证明已经打包成模块,可以进行引用了)

如果这里没有路径,我们在python的shell中使用语句:
sys.path.append(’需要引用模块的地址')
进行添加操作。
2.将已发布的函数模块在另一个文件中引用
step1:编写一个可以具体引用该函数的示例:
并进行具体的引用
import nester ###引用nester包
print(nester.__file__) ###打印nester的位置(当更新模块的时候,可以在此处检查nester的版本)
man = []
other = []
try:
data = open('sketch.txt') ###需要处理的文本
for each_line in data:
try:
(role,line_spoken) = each_line.split(':',1)
"""strip()BIF从字符串中去除不想要的空白符"""
line_spoken = line_spoken.strip()
if role == 'Man':
man.append(line_spoken)
elif role == 'Other Man':
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print('The datafile is missing!')
try:
with open('man_data',"w") as man_file:
nester.print_lol(man, fh=man_file) ###引用函数,注意前面加的nester,相当于人类的”姓“
with open('other_data',"w") as other_file:
nester.print_lol(other, fh=other_file)
except IOError as err:
print('File error: '+str(err))
step2:按F5运行。
输出位置,并创建了对应的文件(第一个和第三个)(此时文件格式还不是适合阅读的状态,可以调动其他函数进行修改)

3986

被折叠的 条评论
为什么被折叠?



