简体   繁体   English

如何在python中的字符串中找到大写字母

[英]how to find capital letters in a string in python

I want to find the capital letters in a string at any position, I want that if a string consists of capital letters at any position in a string then against that string "1" should be print and if string does not containing any capital letter at any position then "0" should be print against that string. 我想在任何位置的字符串中找到大写字母,我希望如果字符串在字符串中的任何位置上都包含大写字母,则应该打印该字符串“ 1”,并且如果字符串中不包含任何大写字母应该在该字符串上打印“ 0”的任何位置。 for this i write a python code but it does not work properly 为此,我编写了python代码,但无法正常工作

file='C:/Python26/test.txt'
f=open('letters.txt','w')
pattern='[A-Z+]'
with open(file, 'r') as rf:

    for word in rf:
        for i in word.split():
            if word[0].isupper():               ## finding letters starting with uppercase letters
                  f.write(word.strip("\n")+"\t"'1'"\n");
            elif word.isupper():                ## finding string containing all capital letters
                  f.write(word.strip("\n")+"\t"'1'"\n");
            elif re.search(pattern, word):      ## finding string containing capital letter at any position 
                  f.write(word.strip("\n")+"\t"'1'"\n");
            else:
            f.write(word.strip("\n")+"\t"'0'"\n");
    f.close()

my exemplary data is like this 我的示例数据是这样的
Src Src
mAB 单抗
32DC32 32DC32
P50 P50
The
activation 激活
fan 风扇
.

NFKappaB NFκB
IL23RE IL23RE
cat
.

but my out put is like this 但是我的输出是这样的

Src 1 源1
mAB 1 单克隆抗体1
32DC32 1 32DC32 1
P50 1 P50 1
The 1 1
activation 0 激活0
fan 0 风扇0
. 0 0
1 1个
NFKappaB 1 NFκB1
IL23RE 0 IL23RE 0
cat 0 猫0
.
Which produce wrong result. 产生错误的结果。 It does not cater the white space and gave the title "1" and because of this nelection the period (.) did not get any label neither of "0" nor of "1" 它不能满足空白要求,标题为“ 1”,由于这种选择,句点(。)既没有获得“ 0”也没有获得“ 1”的任何标签。

Just use re.search instead of re.match because re.match tries to match from the beginning of the string. 只需使用re.search而不是re.match因为re.match尝试从字符串的开头进行匹配。

import re
file='infile'
f=open('outfile','w')
pattern='[A-Z]'
with open(file, 'r') as rf:
    for word in rf:
        if re.search(pattern, word):
            f.write(word.strip() + " 1\n")
        else:
            f.write(word.strip() + " 0\n")
f.close()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM