简体   繁体   English

Python 检查字符串是否有字符

[英]Python check if a string has characters

I am processing 500GB of data to find only the lines which has Characters.我正在处理 500GB 的数据以仅查找具有字符的行。 Can you suggest me a efficent/Faster way.你能建议我一种有效/更快的方式吗?

Data is like:
%^^%^^%^^%
This is a valid 
*%^%^ Valid
This is not a valid one

output should be:输出应该是:

Data is like:
This is a valid 
*%^%^ Valid
This is not a valid one

I am trying this: isalpha() issue is it will remoce the line *%^%^ Valid我正在尝试这个: isalpha() 问题是它会删除该行 *%^%^ 有效

Actuall some how this code is also not working实际上这个代码也不起作用

if line.isalpha()=='True':
  print(line)

This is not working...这不起作用...

can I use regular expressions but read some where it will slow it is that true?我可以使用正则表达式但阅读一些它会减慢的地方是真的吗?

Use regex, like:使用正则表达式,例如:

>>> import re
>>>
>>> pattern = re.compile(r'\A[%|\^]*$')
>>>
>>> pattern.match('%^ Text')  # no match
>>> pattern.match('%^^%^')  # match
<re.Match object; span=(0, 5), match='%^^%^'>

You are not using isalpha correctly, it returns True when all characters in the string are alphabetic.您没有正确使用isalpha ,当字符串中的所有字符都是字母时,它返回True

You could try using any and map to make sure at least one character is alphabet in the line.您可以尝试使用anymap以确保该行中至少有一个字符是字母。

txt = """
Data is like:
%^^%^^%^^%
This is a valid 
*%^%^ Valid
This is not a valid one
"""

for line in txt.split("\n"):
    if any(map(str.isalpha, line)):
        print(line)

prints:印刷:

Data is like:
This is a valid 
*%^%^ Valid
This is not a valid one

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

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