views:

654

answers:

6

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+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"]

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+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"]
extract_the_info, len(lineList)  8
line value :  ORG+1A+77499505:PARAF0103+++A+FR:EUR++11730788+1A'&
ediMsg     :   ORG+1A+77499505:PARAF0103+++A+FR:EUR++11730788+1A'&
count  2

line value :  DUM'&
 DUM'&     :   ORG+1A+77499505:PARAF0103+++A+FR:EUR++11730788+1A'&
count  3

line value :  FPT+CC::::::::N'&
 FPT+CC::::::::N'&+1A+77499505:PARAF0103+++A+FR:EUR++11730788+1A'&
count  4

line value :  CCD+CA:5132839000000027:0450'&
 CCD+CA:5132839000000027:0450'&PARAF0103+++A+FR:EUR++11730788+1A'&
count  5

line value :  CPY+++AF'&
 CPY+++AF'&2839000000027:0450'&PARAF0103+++A+FR:EUR++11730788+1A'&
count  6

line value :  MON+712:1.00:EUR'&
 MON+712:1.00:EUR'&00027:0450'&PARAF0103+++A+FR:EUR++11730788+1A'&
count  7

 MON+712:1.00:EUR'&00027:0450'&FR:EUR++11730788+1A'&

Why is it doing so!?

+10  A: 

You should use the following and forget about this nightmare:

''.join(list_of_strings)
SilentGhost
IceMan85
I really have no idea why it is behaving in this way !!!!
IceMan85
ediMsg = "".join(lineList[1:])That's joining all but the first element together into one string.
stefanw
SilentGhost
-1. Answer doesn't address the REAL problem: using \r instead of \n
John Machin
this is not a problem, this is what he sees at his stdout.
SilentGhost
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.
John Machin
his problem was that he used an awful code, that might have been to blame for the result. once no space for an error was left, it would become obvious that it wasn't a problem in the code. I do understand that you and many other on here advocate spoon-feeding. I don't. thankyouverymuch, bye.
SilentGhost
+3  A: 

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.

googletorp
+23  A: 

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.

balpha
I never knew that's how carriage return works, thanks for the tip.
Tim McNamara
+8  A: 

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.

Andre Miller
Thanks, you were right !I have to remove the \rs ...
IceMan85
+6  A: 

'\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])
user9876
+5  A: 

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"
]
mtasic
This assumes he can change the strings and that the \r is not required for whatever system it will be passed to next.
Andre Miller