简体   繁体   English

根据列表中的项目,打开具有类似名称的文件并将内容附加到df

[英]Based on item in list, open file with similar name and append contents to df

Given myList = ['a', 'b', 'c'] and a folder full of files like 给定myList = ['a', 'b', 'c']和一个充满文件的文件夹,例如

['a_3432.bed', 'a_4958.bed', 'b_3432.bed', 'b_4958.bed', 'c_3432.bed', 
'c_4958.bed', 'd_3432.bed', 'd_4958.bed', etc]

I want to open the files and append their contents to a dataframe based on each item in my list. 我想打开文件,然后根据列表中的每个项目将其内容附加到数据框。 Something like: 就像是:

import pandas as pd

for item in myList:
    open('a*.bed') as infile:
        df = pd.DataFrame()
        for line in infile:
            df.append(line)

I have not used regular expressions, but I think they might be the key because I know they're used to recognize patterns. 我没有使用过正则表达式,但我认为它们可能是关键,因为我知道它们已用于识别模式。

您希望标准的glob模块找到匹配的文件(glob模式,就像您的伪代码中的模式一样,与正则表达式相似但不相同),并可能使用fileinput来将它们全部在一起。

暂无
暂无

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

相关问题 如何打开文件并将项目附加到Python中的现有列表 - How can I open a file and append an item to an existing list in Python 根据列表项内容(字符串)拆分列表 - Splitting a list based on list item contents (string) 根据文件夹中是否存在来自 df 的文件名,在 df 中删除行 - Drop rows in df based on if file name from df exists in folder 遍历 df 行和 append 到没有名称和 dtype 的列表 - Iterate through df row and append to a list w/o name and dtype 将添加内容追加到第一个列表项,而不是将新项目添加到列表 - append adding contents to first list item, not adding new items to list 根据关键字搜索列表到 append 特定列表内容 - Search a list based on key word to append specific list contents 如何打开文本文件并查找在一行中的特定单词和 append 文件名之后写入的内容到 Python 中的列表 - How to open a text file and find what is written after a specific word in a line and append that files name to a list in Python append.json 文件如何由带有 open() append 模式的列表组成? - How to append .json file consist of list with open() append mode? 根据列表中的相似项检查列表的索引,然后根据列表的维度将其追加到另一个列表中 - checking the index of a list according to similar items within the list and then append into another list based on the dimension of the list 有条件地附加文件内容 - Conditionally append file contents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM