954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

a strange list problem about format control

Hi, there

I have been confused about format control of List. My problem is that I get strange format when exchanging List[-1] with List[-2] using a middle variable. My code is to handle a text file.

Old code in a For loop is:
sinf.write('*element,type=s4r,elset=WSLAB\n')
path4b2_value[0]=str(int(path4b2_value[0])+100000)
path4b3=','.join(path4b2_value)
sinf.write(path4b3)
which produces a proper format *element,type=s4r,elset=WSLAB
100001,6,4719,12,6308
*element,type=s4r,elset=WSLAB
100002,4719,2328,6308,2327

However I need to exchange the location of the final two elements in the List, so I changes my code as

The corresponding new code in the For loop is: sinf.write('*element,type=s4r,elset=WSLAB\n')
path4b2_value[0]=str(int(path4b2_value[0])+100000)
path4b2_temp=path4b2_value[-1]
path4b2_value[-1]=path4b2_value[-2]
path4b2_value[-2]=path4b2_temp
path4b3=','.join(path4b2_value)
path4b3=path4b3+'\n'
sinf.write(path4b3)
which prodcues *element,type=s4r,elset=WSLAB
100001,6,4719,6308
,12
*element,type=s4r,elset=WSLAB
100002,4719,2328,2327
,6308

Yes I did exchange the location of the final two element of the list. However I got strange format.

I cannot understand threason why this phenomenon happens. Could you please help me out?

best wishes

ning

ning2009
Junior Poster in Training
58 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Nobody is going to want to read your code when you don't use code tags.

scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

Hi scru

Thank you your comment. Let me put my question again here.

Hi, there

I have been confused about format control of List. My problem is that I get strange format when exchanging List[-1] with List[-2] using a middle variable. I used the code to handle a text file.

Old code in a FOR loop is:

sinf.write('*element,type=s4r,elset=WSLAB\n')
path4b2_value[0]=str(int(path4b2_value[0])+100000)
path4b3=','.join(path4b2_value)
sinf.write(path4b3)


which produces a proper format*element,type=s4r,elset=WSLAB
100001,6,4719,12,6308
*element,type=s4r,elset=WSLAB
100002,4719,2328,6308,2327

However I need to exchange the location of the final two elements in the List, so I changes my code as

New code in the FOR loop is:

sinf.write('*element,type=s4r,elset=WSLAB\n')
path4b2_value[0]=str(int(path4b2_value[0])+100000)
path4b2_temp=path4b2_value[-1]
path4b2_value[-1]=path4b2_value[-2]
path4b2_value[-2]=path4b2_temp
path4b3=','.join(path4b2_value)
path4b3=path4b3+'\n'
sinf.write(path4b3)


which prodcues*element,type=s4r,elset=WSLAB
100001,6,4719,6308
,12
*element,type=s4r,elset=WSLAB
100002,4719,2328,2327
,6308

Yes I did exchange the location of the final two element of the list. However I got strange format.

I cannot understand threason why this phenomenon happens. Could you please help me out?

best wishes

ning

ning2009
Junior Poster in Training
58 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Hi, there

Let me add something here.
I cannot understand why
path4b3=','.join(path4b2_value)
introduces "\n" in the out put file. How does it happen?

best wishes

ning

ning2009
Junior Poster in Training
58 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Hi, there

I sorted it out by adding
path4b3=path4b3.replace('\n','') into my code.
However I still cannot undertstand why.

Thank you.

ning

ning2009
Junior Poster in Training
58 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

which prodcues

*element,type=s4r,elset=WSLAB 100001,6,4719,6308 ,12 *element,type=s4r,elset=WSLAB 100002,4719,2328,2327 ,6308

It appears that you have a newline in with the final element of the list, so after the exchange, the next to the last element prints the newline which puts the last element on the next line. You should .strip() before appending to the list which will get rid of the newline.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 
It appears that you have a newline in with the final element of the list, so after the exchange, the next to the last element prints the newline which puts the last element on the next line. You should .strip() before appending to the list which will get rid of the newline.


Hi, woooee,

Thank you for your comment. Generaly speaking, I understand newline made trouble and however still feel confused when and where newline is generated.

Have a nice weekend.

ning

ning2009
Junior Poster in Training
58 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You