How to put symbols into a list

Question:

How do I put symbols such as , /, :, *, ?, “, <, >, | into a list?

if I do this:

illegalchar = ['', '/' ,':' ,'*', '?', '"', '<', '>', '|']

the commas separating the items will be counted as a string including the ]

PS: It is to check if the filename contains illegal characters (can’t be made into a file), so if there’s any alternative methods, please do tell me, thanks!

Asked By: jeffng50

||

Answers:

You need to escape the special characters like in the string before inserting them into the array, like this:

In [2]: something = ["\", "/"]
In [3]: something
Out[3]: ['\', '/']

Printing it will give you the escaped backslash

In [12]: something = ["\", "/"]
In [13]: something
Out[13]: ['\', '/']
In [14]: print ', '.join(something)
, /
Answered By: DogEatDog

Use raw strings (denoted by putting an r in front of your strings). Then, transform your raw string into list:

illegals = [i for i in r'/:*?"<>|']

# OR, using @abccd's suggestion, just use list()

illegals = list(r'/:*?"<>|')

illegals
# ['\', '/', ':', '*', '?', '"', '<', '>', '|']

Note the '\' when printed is still a single backslash, but in value the first backslash is stored as an escape character.

You can read more on the documentation of lexical analysis.

This answers the question, but in reality you a string is treated like a list of characters, so both of the following will return the same elements:

[i for i in list(r'/:*?"<>|')]
[c for c in  r'/:*?"<>|']

As for how to identify if a filename has any of these characters, you can do this:

valid_file = 'valid_script.py'
invalid_file = 'invalid?script.py'

validate = lambda f: not any(c for c in r'/:*?"<>|' if c in f)

validate(valid_file)
# True

validate(invalid_file)
# False

This is just one of the many ways. You might even opt for a regex approach:

import re

# Note in regex you still need to escape the slash and backslash in the match group
validate = lambda f: not re.search(r'[\/:*?"<>|]+', f)

validate(valid_file)
# True

validate(invalid_file)
# False
Answered By: r.ook

If the idea is just to check for illegal characters, you are doing an overkill here with complex stuffs. python string allow for lookup as they are iterators too. I would go with below easy approach :

In [5]: illegalchar = '/:*?"<>|'

In [6]: if "/" in illegalchar:
            print("nay")
   ...:
nay

downside : one type of quotes have to be skipped which surround the string (' in this case)

Answered By: NoobEditor

Just add the escape character before the backslash .

Change

illegalchar = ['', '/' ,':' ,'*', '?', '"', '<', '>', '|']

to

illegalchar = ['\', '/' ,':' ,'*', '?', '"', '<', '>', '|']
Answered By: Vikram Hosakote

In general I use l = list(r'/:*?"<>|')

However, there are a few sites that do not accept these kind of symbols so I just copied one for a wiki article:

symbols = ['!','#','$','&','(',')','*','+'] => These are the symbols that are accepted on the majority of the platforms

symbols = ['!','#','$','&','(',')','*','+']

Answered By: AllexPwns
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.