Shan-x
Shan-x

Reputation: 1176

How to find what is a character

I have a string cb (from an input I do not control):

foo     

    bar

If I transform this string into a list:

>>> print(cb.splitlines())
['foo \t', '', '    bar']

I need the \t, but not the empty string nor the leading (and potentially trailing) spaces. So I trim a little:

cb_formatted = list(filter(None, cb.splitlines()))
for l in cb_formatted:
    l = l.strip()

But then:

>>> print(cb_formatted)
['foo \t', '    bar']

The leading spaces are still here! So maybe those aren't spaces... But what are they?

So I do this:

    cb_formatted = list(filter(None, cb.splitlines()))
    print(cb_formatted)
    for l in cb_formatted:
        l = l.strip()
        for c in l:
            print(c + "-" + ord(c))

But then:

Traceback (most recent call last):
  File ".\foobar.py", line 61, in <module>
    print(c + "-" + ord(c))
TypeError: must be str, not int

I tried ord(str(c)) with no more luck.

How can I find what are those characters ?

And, optionnally, is there a better method than strip() to trim them?

Upvotes: 1

Views: 91

Answers (5)

johk95
johk95

Reputation: 953

You can't concatenate string and integer objects. ord(c) returns a integer (number). Try:

print(c + "-" + str(ord(c)))

Also, strip takes a argument which is a string defining all the characters that should be trimmed away: https://docs.python.org/2/library/string.html#string.strip

Upvotes: 3

Chris
Chris

Reputation: 22953

You can accomplish your output by using str.strip for each element in your list, and storing the resultant new string. Assuming you have a list of lines:

>>> lines = ['foo \t', '', '    bar']
>>> [s.strip(' ') for s in lines if s]
['foo \t', 'bar']

Upvotes: 0

cco
cco

Reputation: 6281

When you strip the elements of the list, you're creating new strings, but not changing the list.

To get the result you want, use this:

cb_formatted = [ l.strip(' ') for l in cb.splitlines() if l ]

Upvotes: 0

Izaak van Dongen
Izaak van Dongen

Reputation: 2545

The original problem stems from this approach:

cb_formatted = list(filter(None, cb.splitlines()))
for l in cb_formatted:
    l = l.strip()

You're expecting assigning to l to modify the list, but it won't. It just assigns to a variable l which has also had assigned to it the string from the list, as strings cannot be mutated. Also, .strip() will also strip a tab character. I think this should produce your desired behaviour:

cb_formatted = [line.strip(" ") for line in filter(None, cb.splitlines())]

Upvotes: 2

Richard Neumann
Richard Neumann

Reputation: 3361

In the lines

for l in cb_formatted:
    l = l.strip()

you set the name of the stripped element to the name you use for the elements in the loop (l). So on each iteration, your last stripped item will be lost and after the loop l will contain the last stripped item. Furthermore you did not change the list itself at all.

For the other error, see @johk95 's answer

Upvotes: 1

Related Questions