【软件工具-python】如何 只把前面的模块名称取出来?

一、

aiohttp 3.8.4
aiosignal 1.3.1
allure-pytest 2.9.45
allure-python-commons 2.9.45
annotated-types 0.6.0
anyio 3.7.1
apipkg 1.5
argon2-cffi 21.3.0
argon2-cffi-bindings 21.2.0
asgiref 3.4.1
asttokens 2.0.5
async-timeout 4.0.2
atomicwrites 1.4.0
attrs 21.4.0
backcall 0.2.0
beautifulsoup4 4.11.1
bleach 4.1.0
blinker 1.7.0
Bottleneck 1.3.4
brotlipy 0.7.0
certifi 2021.10.8
cffi 1.15.0
charset-normalizer 2.0.4
click 8.1.7
colorama 0.4.4
conda 4.12.0
conda-content-trust 0+unknown
conda-package-handling 1.7.3
cryptography 36.0.0
cx-Oracle 8.3.0
cycler 0.11.0
debugpy 1.5.1
decorator 5.1.1
defusedxml 0.7.1
distro 1.8.0
entrypoints 0.4
et-xmlfile 1.1.0
exceptiongroup 1.1.3
execnet 1.9.0
executing 0.8.3
fastjsonschema 2.15.1
Flask 3.0.0
fonttools 4.25.0
frozenlist 1.3.3
h11 0.14.0
httpcore 1.0.2
httpx 0.25.1
idna 3.3
importlib-metadata 7.0.0
importlib-resources 5.2.0
iniconfig 1.1.1
ipykernel 6.9.1
ipython 8.2.0
ipython-genutils 0.2.0
ipywidgets 7.6.5
itsdangerous 2.1.2
jedi 0.18.1
Jinja2 3.1.2
joblib 1.1.0
jsonschema 4.4.0
jupyter 1.0.0
jupyter-client 7.2.2
jupyter-console 6.4.3
jupyter-core 4.9.2
jupyterlab-pygments 0.1.2
jupyterlab-widgets 1.0.0
kiwisolver 1.3.2
MarkupSafe 2.1.3
matplotlib 3.5.1
matplotlib-inline 0.1.2
menuinst 1.4.18
mistune 0.8.4
mkl-fft 1.3.1
mkl-random 1.2.2
mkl-service 2.4.0
mock 4.0.3
multidict 6.0.4
munkres 1.1.4
nbclient 0.5.13
nbconvert 6.4.4
nbformat 5.3.0
nest-asyncio 1.5.5
notebook 6.4.8
numexpr 2.8.1
numpy 1.21.5
openai 1.2.3
openpyxl 3.0.9
packaging 21.3
pandas 1.4.2
pandocfilters 1.5.0
parso 0.8.3
pathlib 1.0.1
pickleshare 0.7.5
Pillow 9.0.1
pip 24.2
pluggy 1.0.0
prometheus-client 0.13.1
prompt-toolkit 3.0.20
pure-eval 0.2.2
py 1.11.0
pycosat 0.6.3
pycparser 2.21
pydantic 2.4.2
pydantic_core 2.10.1
pydub 0.25.1
Pygments 2.11.2
PyMySQL 1.0.2
pyOpenSSL 21.0.0
pyparsing 3.0.4
pyrsistent 0.18.0
PySocks 1.7.1
pytest 7.1.1
pytest-assume 2.4.3
pytest-forked 1.4.0
pytest-rerunfailures 10.2
pytest-xdist 2.5.0
python-dateutil 2.8.2
pytz 2021.3
pywin32 302
pywinpty 2.0.2
PyYAML 6.0
pyzmq 22.3.0
qtconsole 5.3.0
QtPy 2.0.1
requests 2.31.0
robotframework 5.0
ruamel-yaml-conda 0.15.100
scikit-learn 1.0.2
scipy 1.7.3
selenium 3.141.0
Send2Trash 1.8.0
setuptools 58.0.4
sip 4.19.13
six 1.16.0
sniffio 1.3.0
soupsieve 2.3.1
stack-data 0.2.0
tables 3.7.0
terminado 0.13.1
testpath 0.5.0
threadpoolctl 2.2.0
toml 0.10.2
tomli 1.2.2
tornado 6.1
tqdm 4.62.3
traitlets 5.1.1
typing_extensions 4.8.0
urllib3 1.25.11
w3lib 1.21.0
wcwidth 0.2.5
webencodings 0.5.1
Werkzeug 3.0.1
wheel 0.37.1
widgetsnbextension 3.5.2
win-inet-pton 1.1.0
wincertstore 0.2
xlrd 2.0.1
XlsxWriter 3.0.3
xlwings 0.24.9
xlwt 1.3.0
yarl 1.9.2
zipp 3.7.0
你可以使用 Python 脚本来提取出这些模块的名称。假设你有一个包含上述内容的文本文件(例如 modules.txt),你可以使用以下方法只提取模块名称:

方法一:使用正则表达式

import re

# 打开包含模块列表的文件
with open('modules.txt', 'r') as file:
    lines = file.readlines()

# 正则表达式匹配模块名称(包括空格前的内容)
module_names = [re.split(r'\s+', line.strip())[0] for line in lines if line.strip()]

# 打印模块名称
for module in module_names:
    print(module)

方法二:使用字符串分割

# 打开包含模块列表的文件
with open('modules.txt', 'r') as file:
    lines = file.readlines()

# 提取每一行中的模块名称
module_names = [line.split()[0] for line in lines if line.strip()]

# 打印模块名称
for module in module_names:
    print(module)

方法三:处理直接的列表

如果你已经将模块列表保存到一个列表中,你可以直接使用以下方法:

modules = [
    "aiohttp 3.8.4",
    "aiosignal 1.3.1",
    "allure-pytest 2.9.45",
    # 其他模块省略...
    "yarl 1.9.2",
    "zipp 3.7.0"
]

# 提取模块名称
module_names = [module.split()[0] for module in modules]

# 打印模块名称
for name in module_names:
    print(name)

输出

这些代码都会输出如下内容:

aiohttp
aiosignal
allure-pytest
allure-python-commons
annotated-types
anyio
apipkg
...
zipp

解释

  • line.strip(): 去除每一行的前后空白字符。
  • line.split(): 按空白字符分割每一行,split()[0] 返回第一个元素,即模块名称。
  • 正则表达式 re.split(r'\s+', line.strip()): 更高级的分割方式,处理不同数量的空格。

你可以根据需要保存或处理提取出来的模块名称列表。

  • 15
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿寻寻

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值