1

I have this little problem in Python when using

print ",".join(array)

For example, when using array=['dog\r', 'monkey', 'pig'] the output looks like this:

,monkey,pig

Any ideas?

Ok guys the code istself looks like this

a = raw_input(">>")
uAlpha = a.split(",")

Now I know how to remove the /r but how to prevent it from getting there in the first place ?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
ign699
  • 21
  • 2
  • Simple: just remove the carriage return. – Martijn Pieters May 11 '15 at 19:46
  • The problem is that this new array is created when splitting output and python adds the /r by itself to the last string in array. Any way to prevent this from happening automatically ? – ign699 May 11 '15 at 19:48
  • 1
    Python does no such thing, it doesn't just add `\r` characters without cause. Can you show us how you got the input in the first place? – Martijn Pieters May 11 '15 at 19:50
  • a = raw_input(">>") uAlpha = a.split(",") When doing this last string in uAlpha ends with /r. – ign699 May 11 '15 at 19:52
  • @ign699 Python does not add the `\r`; your command shell (Windows, presumably) is passing a `\r\n`-terminated string to Python's standard input. – chepner May 11 '15 at 19:58
  • @ign699: and how are you passing in the data to Python? Is this coming from some other subsystem? – Martijn Pieters May 11 '15 at 20:00
  • I run the code through notepad++ using python -u -i $(FULL_CURRENT_PATH). – ign699 May 11 '15 at 20:02

3 Answers3

8

\r is a carriage return character, so it is doing exactly what it should. The string is actually dog\r,monkey,pig, but when you print it, it looks like ,monkey,pig due to what \r does (moves the cursor to the beginning of the line). Try this to strip the carriage return (and any other white space) from the strings:

>>> array=['dog\r', 'monkey', 'pig']
>>> ",".join(s.strip() for s in array)
dog,monkey,pig

Alternatively, if you posted your code, we might be able to help you to get the "\r" from being on there in the first place.

Jake Griffin
  • 2,014
  • 12
  • 15
3

You are including a carriage return in your string. You are instructing the terminal to move the cursor back to the start of the line and continue printing from there.

You built the string:

'dog\r,monkey,pig'

and are printing that, which results in first dog being printed, then the cursor moving back to the start of the line (returning the 'printing carriage'), then the rest being printed overwriting the dog text.

Either remove the carriage return from your input, or from your joined string. You could use str.replace() or str.translate() to remove all carriage return characters, or use str.strip() (and variants) to remove them from the start and end of a string when you still have the list.

For example, if your input is coming from raw_input(), you could simply use str.rstrip() to remove carriage returns from the end:

a = raw_input(">>").rstrip('\r')
uAlpha = a.split(",") 
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

The return character was causing it to skip the first part, you can see the difference by using repr

>>> print ",".join(array)
,monkey,pig
>>> print repr(",".join(array))
'dog\r,monkey,pig'

Use map and strip to remove the return character

>>> array=['dog\r', 'monkey', 'pig']
>>> ",".join(map(str.strip,array))

The output is

'dog,monkey,pig'

Note that it is slightly better to use map rather than a comprehension to strip characters. The timeit results

$ python -m timeit "','.join(s.strip() for s in ['dog\r', 'monkey', 'pig'])"
1000000 loops, best of 3: 1.18 usec per loop
$ python -m timeit "','.join(map(str.strip,['dog\r', 'monkey', 'pig']))"
1000000 loops, best of 3: 0.647 usec per loop

The use of map vs comprehensions is discussed here.

The most popular (and accepted answer) conclusion is quoted here:

map may be microscopically faster in some cases (when you're NOT making a lambda for the purpose, but using the same function in map and a listcomp).

Community
  • 1
  • 1
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140