python - 如何在python中导入仅包含某些字符串的“dat”文件
问题描述
我正在尝试将几个“dat”文件导入 python spyder。
这里列出了dat文件,列表中有两种以_1和_2结尾的类型。我只想导入以“_1”结尾的 dat 文件。
有没有办法通过一个循环一次导入它们?
导入它们后,我想将所有这些聚合到一个矩阵中。
解决方案
import os
files_to_import = [f for f in os.listdir(folder_path)
if f.endswith("1")]
Make sure that you know whether the files have a .dat-extension or not - in Windows Explorer, the default setting is to hide file endings, and this will make your code fail if the files have a different ending.
What this code does is called list comprehension - os.listdir() provides all the files in the folder, and you create a list with only the ones that end with "1".
推荐阅读
- regex - 检查有效的部分散列字符串
- php - 如何组合这两个数组?
- python - 在页面加载中设置 pyqt5 上的表值会引发 AttributeError
- c++ - 前向声明带有派生类模板参数的函数
- java - JSON 对象到 BODY 发布者
- javascript - 获取输入框的值并将其放入“let”变量中
- python - 我的随机替换系统替换了错误的项目
- php - 将特定的 XML 转换为 PHP 数组
- excel - VBA CODE RUNTIME ERROR 1004 表示对象定义错误
- java - 在 Cassandra 中使用 sstabledump 时 /tmp 挂载为 noexec 时出现 java.lang.UnsatisfiedLinkError