[英]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.