Find usernames which are sorter or equal than 3 characters and save it to a text file python

Question:

So, I have a file which looks like this:

@d | [email protected]:joh22nny!! - Number of visits: 14814 - True - False - False
@hannahiscool |[email protected]:h1annah!! - Number of visits: 132 - True - False - True
@pokemon |[email protected]:gin55er.! - Number of visits: 15 - True - False - False
@sh | [email protected]:short - Number of visits: 14814 - True - False - False

I want to sort all usernames that I input from a text file so it will write usernames shorter than 3 characters to a output file.
The only way I found to do that is to do a len(), but gmails and password won’t have the same length. My objective is to make it look like this:

#shorter_than_3_characters.txt
@d | [email protected]:joh22nny!! - Number of visits: 14814 - True - False - False
@sh | [email protected]:short - Number of visits: 14814 - True - False - False
#4_character_or_more.txt
@hannahiscool |[email protected]:h1annah!! - Number of visits: 132 - True - False - True
@pokemon |[email protected]:gin55er.! - Number of visits: 15 - True - False - False

For clarification, a username on this file looks like:
@{username_here}

There’s no code because, I couldn’t find anything that would do that on regex, so I thought someone else may know more about regex and etc.

Asked By: Wiki

||

Answers:

you can just use split() method

if len( line.split(' | ')[0] ) < 5:

if you have to use regex try something like this

if re.match(re.compile("@[a-zA-z]{1,3}s|s"), line)
Answered By: iamtrappedman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.