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

Re: a strange list problem about format control 80 80

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

Re: a strange list problem about format control 80 80

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

Re: a strange list problem about format control 80 80

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

Re: a strange list problem about format control 80 80

Hi, there

I sorted it out by adding

path4b3=path4b3.replace('\n','')

into my code.
However I still cannot undertstand why.

Thank you.

ning

Re: a strange list problem about format control 80 80

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.

Re: a strange list problem about format control 80 80

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.