简体   繁体   English

如何通过创建掩码从文件夹中导入某些文件?

[英]How can I import certain files from a folder by creating a mask?

I'm incredibly new to coding so please bear with me so basically I have a folder of 4,229 .fits files (legac_spec) and a dataframe (legac_cat) with 1989 rows, two of the columns being id number and mask value.我对编码非常陌生,所以请耐心等待,所以基本上我有一个包含 4,229 个 .fits 文件(legac_spec)的文件夹和一个包含 1989 行的数据框(legac_cat),其中两列是 ID 号和掩码值。 Each .fits file has a file name of something along the lines of legac_M[mask value]_v3.6_spec1d _[id number].fits but I'm not sure how to get the specific files I need where each mask value and id number corresponds to a specific file.每个 .fits 文件都有一个类似于legac_M[mask value]_v3.6_spec1d _[id number].fits的文件名,但我不确定如何获取我需要的特定文件,其中每个掩码值和 ID 号对应一个特定的文件。

I know I need to use a for loop but I'm not sure how I get it to do what i need it to do since I have to mask the mask value and id numbers separately我知道我需要使用 for 循环,但我不确定如何让它做我需要做的事情,因为我必须分别屏蔽掩码值和 ID 号

import numpy as np
import pandas as pd
from astropy.io import fits

legac_cat = pd.read_csv('legac_file')

M = legac_cat['mask']
ID = legac_cat.id

directory_name = 'C:/Users/kfdhfs/Downloads/legac_spec'
for mask_val in legac_cat['mask']:
     for files in directory_name:
          hdu = fits.open(files)

I've barely ever used Pandas.我几乎没用过 Pandas。 I haven't tested this solution, so sorry if it doesn't work "straight out of the box".我还没有测试过这个解决方案,如果它不能“直接开箱即用”,那么很抱歉。 It seems to me that all you have to do is iterate over your dataframe's rows (where each row has a "mask" and "id" that correspond to one file), and then construct a filename from each row's "mask" and "id" - then open the file:在我看来,您所要做的就是遍历数据帧的行(其中每一行都有一个对应于一个文件的“掩码”和“id”),然后从每一行的“掩码”和“id”构造一个文件名" - 然后打开文件:

legac_cat = pd.read_csv("legac_file")

for index, row in legac_cat.iterrows():

    file_name = "legac_M{}_v3.6_spec1d_{}.fits".format(row["mask"], row["id"])
    # open the file using file_name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何将python模块安装到某个文件夹中并将其导入 - Python: How can I install a python module into a certain folder and import it 我可以一次从一个文件夹中导入所有 Python 文件吗? - Can I import all Python files from a folder at once? 如何打印文件夹/子文件夹并从中导入文件? - How can I print folder/sub-folders and import files from them? 在Python中,如何从具有相对路径的祖父母文件夹中导入? - In Python, how can I import from a grandparent folder with a relative path? 如何从 python 中的另一个文件夹导入 csv? - How can I import a csv from another folder in python? 如何从没有错误的文件夹中导入 Django 中的文件 - How can I import the file in Django from a folder without Errors Python:如何从另一个文件夹导入脚本,而该文件夹又在本地导入具有相对路径/本地文本文件的其他脚本? - Python: How can i import a script from another folder that also locally imports other scripts with relative paths/local text files? 如何从python中的另一个文件夹导入文件? - How to Import files from another folder in python? 如何将多个文件夹层次结构中的所有 python 文件导入单个 pyinstaller 可执行文件? - How can I import all my python files in multiple folder hierarchy into a single pyinstaller executable? 如何将文件从一个文件夹复制到多个文件夹 - How can I copy files from a folder into multiple folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM