返回: “如何编写Python代码以返回特定的字母和数字组合?”

huangapple go评论136阅读模式
英文:

How can I write a Python code to return a certain combination of letters and numbers?

问题

我需要从数据集中提取特定的元数据。我只对键为字母'p'后跟一个整数,然后是另一个字母's'的特定字段感兴趣。

例如,我的第一个数据集具有以下字段,我需要提取:

P7s
P8s
P9s
p10s
p12s

是否有办法编写代码来返回这些项?

这是我目前的代码,我在其中指定数据集中的内容与我加载文件的应用程序中所需的元数据形式匹配的内容。

问题出在 'Markers analysed',我想要指定如果 'Markers analysed' = p#s,然后返回该值。

metadata_crosswalk={
    'Date of Analysis': 'date',
    'Creator':'export user name',
    'Instrument':'cyt',
    'Markers analysed': ,   # 这里有问题
    'Number of Samples':'tot'
}

for coscine_key,fcs_key in metadata_crosswalk.items():
    print("looking at metadata key (coscine)", coscine_key)
    print("looking to store fcs metadata key", fcs_key, "| value:", fcs_metadata[fcs_key])
    metadata[coscine_key]=fcs_metadata[fcs_key]
英文:

I have to pull certain metadata from a data set. I'm only interested in a certain field whose key is the letter 'p' followed by an integer and then another letter 's'.

For example, my first dataset has the following fields that I need to extract:

P7s
P8s
P9s
p10s
p12s

Is there anyway to write a code to return those items?

This is my code right now where I specify what in the data set will match with the required metadata form in the application where I'm loading the files.

The issue is with 'Markers analysed' where I would like to specify if 'Markers analysed'= p # s then return that value.

metadata_crosswalk={
    'Date of Analysis': 'date',
    'Creator':'export user name',
    'Instrument':'cyt',
**    'Markers analysed': ,**
    'Number of Samples':'tot'
}

for coscine_key,fcs_key in metadata_crosswalk.items():
    print("looking at metadata key (coscine)", coscine_key)
    print("looking to store fcs metadata key", fcs_key, "| value:", fcs_metadata[fcs_key])
    metadata[coscine_key]=fcs_metadata[fcs_key]

答案1

得分: 2

import re

pattern = re.compile(r'p\d+s')

for coscine_key, fcs_key in metadata_crosswalk.items():
    print("查找元数据键(coscine):", coscine_key)
    if fcs_key is not None:
        print("查找要存储的fcs元数据键:", fcs_key, "| 值:", fcs_metadata[fcs_key])
        metadata[coscine_key] = fcs_metadata[fcs_key]
    elif coscine_key == 'Markers analysed':
        matching_fields = [field for field in fcs_metadata.keys() if pattern.match(field)]
        print("匹配的字段:", matching_fields)
        metadata[coscine_key] = matching_fields
英文:

You can use regular expressions to extract the fields.

import re

pattern = re.compile(r'p\d+s')

for coscine_key,fcs_key in metadata_crosswalk.items():
    print("looking at metadata key (coscine)", coscine_key)
    if fcs_key is not None:
        print("looking to store fcs metadata key", fcs_key, "| value:", fcs_metadata[fcs_key])
        metadata[coscine_key]=fcs_metadata[fcs_key]
    elif coscine_key == 'Markers analysed':
        matching_fields = [field for field in fcs_metadata.keys() if pattern.match(field)]
        print("matching fields:", matching_fields)
        metadata[coscine_key] = matching_fields

huangapple
  • 本文由 发表于 2023年3月9日 15:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定