简体   繁体   English

如何在 Python 中将字符串的第一个字母大写,而忽略 HTML 标签?

[英]How can I capitalize the first letter of a string in Python, ignoring HTML tags?

I would like to capitalize the first letter of a string, ignoring HTML tags.我想大写字符串的第一个字母,忽略 HTML 标签。 For instance:例如:

<a href="google.com">hello world</a>

should become:应该变成:

<a href="google.com">Hello world</a>

I wrote the following, which works, but it seems inefficient, since every character of the string is being copied to the output.我写了以下内容,它有效,但似乎效率低下,因为字符串的每个字符都被复制到输出中。 Is there a better way to do it?有没有更好的方法来做到这一点?

@register.filter
def capinit(value):
  gotOne = False
  inTag = False
  outValue = ''
  for c in value:
    cc = c
    if c == '<':
      inTag = True
    if c == '>':
      inTag = False
    if not inTag:
      if c.isalpha() or c.isdigit():
        if not gotOne:
          cc = c.upper()
        gotOne = True
    outValue = outValue + cc
  return outValue

Note that this ignores initial punctuation.请注意,这会忽略初始标点符号。 It will capitalize the first letter it finds, unless it finds a number first in which case it doesn't capitalize anything.它将大写它找到的第一个字母,除非它首先找到一个数字,在这种情况下它不会大写任何东西。

I tried to do what you wanted:我试图做你想做的事:

html = '<a href="google.com">hello world</a>'

afterletter = None
dontcapital = 0
afterhtml = ""
for character in html:
    if character == "/" and afterletter == "<":
        afterhtml += character
        dontcapital = 1
    elif afterletter == ">":
        if dontcapital == 0:
            afterhtml += character.upper()
        else:
            afterhtml += character
            dontcapital = 0
    else:
        afterhtml += character
    afterletter = character

print(afterhtml)

#afterhtml is the output!

this should work from all the tests i did.这应该适用于我所做的所有测试。
if anyone wants to work on it you can.如果有人想研究它,你可以。

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

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