【问题标题】:Why does this string not change to uppercase?为什么这个字符串不会变成大写?
【发布时间】:2015-07-02 18:16:59
【问题描述】:

所以我有一个氨基酸文件,我正在尝试阅读 mdvfmkglskakegvvaaaektkqgvaeaagktkegvlyvgsktkegvvhgvatvaektk eqvtnvggavvtgvtavaqktvegagsiaaatgfvkkdqlgkneegapqegiledmpvdp dneayempseegyqdyepea

我有一个称为氨基酸的大写字母列表。问题是我无法阅读序列,因为字母是小写的。我一直在尝试将其设为大写。读取文件没有问题,我认为我已成功将其内容转换为字符串(但也许我没有?)。

aminoacids = ['A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y']
content1 = fh.readline() #first line, which is not the sequence
        #print content1
charline1 = len(content1)-1 #number of characters in the first line
        #print charline1
contentall = fh.readlines() #each line is converted into a string and put into a list
        #print contentall
numlines = len(contentall) #number of elements in list = number of lines, not the first one
        #print numlines
contentjoined = ''.join(contentall) #list elements are combined, but this includes new lines as characters
contentjoined = contentjoined.translate(None, "\n")
contentjoined = contentjoined.translate(None,''.join([i for i in contentjoined if i not in aminoacids]))
contentjoined = contentjoined.upper()
print contentjoined
numaa = len(contentjoined)
print numaa #this shouldn't be zero but it is

为什么这不起作用?我能做些什么来修复它?我现在在with 中……以前这不是问题,但现在是吗? Numaa 是 0,而它不应该是。我意识到我可以在列表中添加小写字母,但应该有一种更“pythonic”的方式来解决这个问题。

【问题讨论】:

    标签: python string uppercase


    【解决方案1】:

    是不是因为您在检查aminoacids 中的字符串后将字符串设置为大写?尝试将contentjoined = contentjoined.upper() 向上移动一两行。

    当您检查aminoacids 时,您为str.translate 提供了一个完全小写的字符串,因此它与字符串不匹配。它最终看起来像这样:

    >>> c = contentjoined.translate(None,''.join([i for i in contentjoined if i not in aminoacids]))
    >>> c
    ''
    

    如果您首先调用upper,您将比较一个大写字符串与一个大写字符串列表,因此您实际上会有匹配项。它看起来像这样:

    >>> contentjoined = contentjoined.upper()
    >>> c = contentjoined.translate(None,''.join([i for i in contentjoined if i not in aminoacids]))
    >>> c
    'MDVFMKGLSKAKEGVVAAAEKTKQGVAEAAGKTKEGVLYVGSKTKEGVVHGVATVAEKTKEQVTNVGGAVVTGVTAVAQKTVEGAGSIAAATGFVKKDQLGKNEEGAPQEGILEDMPVDPDNEAYEMPSEEGYQDYEPEA'
    

    如果要将字符串保留为小写字母,只需与大写字母进行比较并保留小写字母即可。看起来像这样:

    >>> c = contentjoined.translate(None,''.join([i for i in contentjoined.upper() if i not in aminoacids]))
    >>> c
    'mdvfmkglskakegvvaaaektkqgvaeaagktkegvlyvgsktkegvvhgvatvaektkeqvtnvggavvtgvtavaqktvegagsiaaatgfvkkdqlgkneegapqegiledmpvdpdneayempseegyqdyepea'
    

    【讨论】:

    • 我不敢相信我错过了!我认为我的整个字符串都被更改为None
    【解决方案2】:

    问题在于您的translate() 命令:

    contentjoined = contentjoined.translate(None, "\n")
    contentjoined = contentjoined.translate(None,''.join([i for i in contentjoined if i not in aminoacids]))
    

    在这里,您将用 None 替换找到的所有内容(好吧,我不确定您在 contentjoinedaminoacids 中有什么数据)。 喜欢如果你尝试:

    >>>temp = "this is a test string"
    >>>temp.translate(None, "aeiou")
    >>>'ths s  tst strng' #THIS IS OUTPUT
    

    所以我猜你的整个字符串都变成了None。 查看translate() Docs

    【讨论】:

    • 是的,你可以这么想。整个字符串被更改为None,因为它不匹配任何列表元素。但它与列表元素不匹配的原因是因为它是小写的,这就是为什么我要求将其设为大写。
    【解决方案3】:

    当您拉入文件时,您可以将所有内容转换为大写。也许是这样的?

    with open('myfile.txt', 'r') as f:
        data = f.read().upper()
    
    print(data)
    'MDVFMKGLSKAKEGVVAAAEKTKQGVAEAAGKTKEGVLYVGSKTKEGVVHGVATVAEKTK\nEQVTNVGGAVVTGVTAVAQKTVEGAGSIAAATGFVKKDQLGKNEEGAPQEGILEDMPVDP\nDNEAYEMPSEEGYQDYEPEA\n'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多