2

I have done this operation millions of times, just using the + operator! I have no idea why it is not working this time, it is overwriting the first part of the string with the new one! I have a list of strings and just want to concatenate them in one single string! If I run the program from Eclipse it works, from the command-line it doesn't! The list is:

["UNH+1+XYZ:08:2:1A+%CONVID%'&\r", "ORG+1A+77499505:ABC+++A+FR:EUR++123+1A'&\r", "DUM'&\r"]

I want to discard the first and the last elements, the code is:

        ediMsg = ""
        count = 1
        print "extract_the_info, lineList ",lineList
        print "extract_the_info, len(lineList) ",len(lineList)
        while (count < (len(lineList)-1)):
            temp = ""
#            ediMsg = ediMsg+str(lineList[count])
#            print "Count "+str(count)+" ediMsg ",ediMsg 
            print "line value : ",lineList[count]
            temp = lineList[count]
            ediMsg += " "+temp
            print "ediMsg     : ",ediMsg
            count += 1
            print "count ",count

Look at the output:

extract_the_info, lineList  ["UNH+1+XYZ:08:2:1A+%CONVID%'&\r", "ORG+1A+77499505:ABC+++A+FR:EUR++123+1A'&\r", "DUM'&\r"]
extract_the_info, len(lineList)  8
line value :  ORG+1A+77499505:ABC+++A+FR:EUR++123+1A'&
ediMsg     :  ORG+1A+77499505:ABC+++A+FR:EUR++123+1A'&
count  2

line value :  DUM'&
 DUM'&     :  ORG+1A+77499505:ABC+++A+FR:EUR++123+1A'&
count  3

Why is it doing so!?

4
  • 19
    My eyes!! .... The characters burn! Commented Jun 29, 2009 at 15:26
  • 2
    This is probably the worst string concatenation in python I've ever seen. Commented Jun 29, 2009 at 15:29
  • So ??? Any suggestion ?? Commented Jun 29, 2009 at 15:31
  • 1
    You also might want to look at the for loop in Python. No need to make this while count thingy. Commented Jun 29, 2009 at 15:38

6 Answers 6

32

While the two answers are correct (use " ".join()), your problem (besides very ugly python code) is this:

Your strings end in "\r", which is a carriage return. Everything is fine, but when you print to the console, "\r" will make printing continue from the start of the same line, hence overwrite what was written on that line so far.

Sign up to request clarification or add additional context in comments.

1 Comment

I never knew that's how carriage return works, thanks for the tip.
21

You should use the following and forget about this nightmare:

''.join(list_of_strings)

8 Comments

Exactly the same problem : code : ediMsg = ''.join(lineList[1:len(lineList)-1]) output : MON+712:1.00:EUR'&00027:0450'&FR:EUR++11730788+1A'&
ediMsg = "".join(lineList[1:]) That's joining all but the first element together into one string.
-1. Answer doesn't address the REAL problem: using \r instead of \n
His problem was that he saw the result of printing \r on his stdout and didn't recognise what was happening. He needed to be told that.
@JohnMachin is right. This answer gives some potentially useful advice, but doesn't address the actual question. It shouldn't have been accepted.
|
11

The problem is not with the concatenation of the strings (although that could use some cleaning up), but in your printing. The \r in your string has a special meaning and will overwrite previously printed strings.

Use repr(), as such:

...
print "line value : ", repr(lineList[count])
temp = lineList[count]
ediMsg += " "+temp
print "ediMsg     : ", repr(ediMsg)
...

to print out your result, that will make sure any special characters doesn't mess up the output.

Comments

8

'\r' is the carriage return character. When you're printing out a string, a '\r' will cause the next characters to go at the start of the line.

Change this:

print "ediMsg     : ",ediMsg

to:

print "ediMsg     : ",repr(ediMsg)

and you will see the embedded \r values.

And while your code works, please change it to the one-liner:

ediMsg = ' '.join(lineList[1:-1])

Comments

7

Your problem is printing, and it is not string manipulation. Try using '\n' as last char instead of '\r' in each string in:

lineList = [
    "UNH+1+TCCARQ:08:2:1A+%CONVID%'&\r",
    "ORG+1A+77499505:PARAF0103+++A+FR:EUR++11730788+1A'&\r",
    "DUM'&\r",
    "FPT+CC::::::::N'&\r",
    "CCD+CA:5132839000000027:0450'&\r",
    "CPY+++AF'&\r",
    "MON+712:1.00:EUR'&\r",
    "UNT+8+1'\r"
]

1 Comment

This assumes he can change the strings and that the \r is not required for whatever system it will be passed to next.
5

I just gave it a quick look. It seems your problem arises when you are printing the text. I haven't done such things for a long time, but probably you only get the last line when you print. If you check the actual variable, I'm sure you'll find that the value is correct.

By last line, I'm talking about the \r you got in the text strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.