Hi, there

I have resource lines and target lines here below

resource lines:
1 J=2,7183 SEC=CON450X450 NSEG=2 ANG=0
56 J=7224,164 SEC=CON450X450 NSEG=2 ANG=0

Target lines:
*element,type=b31,elset=CON450X4501
*element,type=b31,elset=CON450X45056

"elset=CON450X4501" in the target lines come from a combination of "1" and "SEC=CON450X450". Similarly, "elset=CON450X45056" in the target lines come from a combination of "56" and "SEC=CON450X450". How can I achieve this transformation? The steps are supposed to be

1) capture string just after "SEC=", assign in just after "elset="
2) capture first digital number, put it just at the end of strings "elset=CON450X4501"

I am trying to use split and have obtained nothing yet. Could you please help me out? Thank you a lot.

best wishes

ning

What do you mean by "obtained nothing yet"?

I would suggest using a regular expression to group out the SEC= portion and the beginning of the line, but I don't know how consistent that data will appear in said form. Refer here for the re module information

To write a variable to a line in a file you would use something like this:

var1 = 'test1'
var2 = 3456
var3 = 'Something else'
#
fh = open( 'myfile.txt', 'w' )
fh.write( 'var1=%s, var2=%s, var3=%s\n' % ( var1,var2,var3 ) )
fh.close()

I'm assuming that the data in:

1 J=2,7183 SEC=CON450X450 NSEG=2 ANG=0
56 J=7224,164 SEC=CON450X450 NSEG=2 ANG=0

is a string that you have read in from somewhere. If you are using code to generate these strings then you can vastly simplify things. I'm assuming that you also aren't too familiar with python, so I figure that some simple code using a for loop would be the most useful to you.

Under these circumstances you can accomplish your goal as follows:

#Define input variable
in1 = '1 J=2,7183 SEC=CON450X450 NSEG=2 ANG=0\n56 J=7224,164 SEC=CON450X450 NSEG=2 ANG=0' 

#Define constants
OUTPUT_PREFIX = 'elset='

#For each line add the output for the line to output list
lines = in1.split('\n')
output_list = []
for line in lines:
	#I assume that your fields are delimited by spaces
	fields = line.split(' ')
	elset_suffix = fields[0]
	#Slice off the 'Sec=' prefix
	sec_field = fields[2]
	elset_prefix = sec_field[4:]
	
	#join all three strings together
	elset_str = ''.join([OUTPUT_PREFIX, elset_prefix, elset_suffix])
	output_list.append(elset_str)

The variable output_list contains the desired results. This is by no means the best way to handle such a situation, but it should help you understand the logic behind how the solution can be obtained.

Hi, Nine Tails

Thank you so much for your patient help. I did some nuemrical programming in Fortran, Matlab. I really appreciate your help.

I am studying your code and reply you later on

all the best

Hi, jlm699

Thank you for your suggestion.

"obtained nothing yet" means that I am still working on it. Actually I misleaded you and am sorry for that.

Let me explain a little bit more

Resource line
56 J=7224,164 SEC=CON450X450 NSEG=2 ANG=0

Target lines:
*element,type=b31,elset=CON450X45010056

I want to achieve a procedure as follows
1) capture the first number, in the case, 56
2) add a number on 56, for example, 10000+56=10056
3) replace "CON450X450" on the resource line with "CON450X45010056"

Hopefully I make sense now.

all the best

ning

It would be helpful to know how your resource lines are entering your program. Are they entered by the user interactively, read in from a file, created from other variables that exist in your program code, etc.

Also, in your last post you stated that your goal is to perform integer addition

10000 + 56 = 10056

Instead of the string concatenation

'10000' + '56' = '1000056'

that is described in your original post. Integer addition won't work on your SEC values because they contain non-numeric characters.

If for some reason that you did not describe you are also looking to add strings as if they were integers you can accomplish this as follows:

str1 = '10000'
str2 = '56'
#Integer addition solution as an integer
int_total = int(str1) + int(str2)
#Integer addition solution as a string
str_total = str(int_total)

I thought of your problem this WE and wrote a solution.
I see other people have answered but I post mine as it is written anyway. It will give you another way to solve your problem in the same idea i gave to your other post.

lines=["1 J=2,7183 SEC=CON450X450 NSEG=2 ANG=0",
       "56 J=7224,164 SEC=CON450X450 NSEG=2 ANG=0"]
targetLine="*element,type=b31,elset=%s%s"
tLines=[]  # to store the modified target lines
for line in lines:
    resultDic={} # We will store the elements of the line in a dictionary
                  # like this {'id':'1', 'J':'2,7183', 'SEC':'CON450X450'...}
    splittedLine=line.split() # split the line using space
    for element in splittedLine:
        keyValue=element.split("=") # split the element using =
        if len(keyValue)==2:
            resultDic[keyValue[0]]=keyValue[1] # store the element in the
                                # dictionnary this way {"SEC":"CON450X450"}
        elif len(keyValue)==1:
            resultDic['id']=keyValue[0]
    tLines.append(targetLine % (resultDic['SEC'], resultDic['id']))

print "\n".join(tLines)

It would be helpful to know how your resource lines are entering your program. Are they entered by the user interactively, read in from a file, created from other variables that exist in your program code, etc.

Also, in your last post you stated that your goal is to perform integer addition

10000 + 56 = 10056

Instead of the string concatenation

'10000' + '56' = '1000056'

that is described in your original post. Integer addition won't work on your SEC values because they contain non-numeric characters.

If for some reason that you did not describe you are also looking to add strings as if they were integers you can accomplish this as follows:

str1 = '10000'
str2 = '56'
#Integer addition solution as an integer
int_total = int(str1) + int(str2)
#Integer addition solution as a string
str_total = str(int_total)

Hi, guys

Nine tails' code works well. However I got an interesting problem. Shall I show you?

I have resource data shown as
13304,PZ,-4500
13305,PZ,-4500
13306,PZ,-4500
13307,PZ,-4500
13246,PZ,-4500
13247,PZ,-4500
13248,PZ,-4500
17576,PZ,-4500
110,PZ,-4500
111,PZ,-4500
112,PZ,-4500
113,PZ,-4500
114,PZ,-4500
115,PZ,-4500
59,PZ,-4500
60,PZ,-4500
61,PZ,-4500
2351,PZ,-4500
2352,PZ,-4500
2353,PZ,-4500
2354,PZ,-4500
2355,PZ,-4500
2356,PZ,-4500
2293,PZ,-4500
2294,PZ,-4500
2295,PZ,-4500
4633,PZ,-4500
4634,PZ,-4500
4635,PZ,-4500
4636,PZ,-4500
4637,PZ,-4500
4638,PZ,-4500
4579,PZ,-4500
4580,PZ,-4500
4581,PZ,-4500
6813,PZ,-4500
6814,PZ,-4500
6815,PZ,-4500
6816,PZ,-4500

I used the code ina FOR loop below to handle them
path3a_value=path3a.split(',')
path3a_value[0]=str(int(path3a_value[0])+100000)
path3a1=','.join(path3a_value)
aload.write(path3a1)
and got the exact result I want.
100110,P,-1125.0
100113,P,-1125.0
100114,P,-1125.0
100120,P,-1125.0
100121,P,-1125.0
100122,P,-1125.0

However if I use the code in the same For loop below
path3a_value=path3a.split(',')
path3a_value[0]=str(int(path3a_value[0])+100000)
path3a_value[2]=str(float(path3a_value[2])*0.25)
path3a1=','.join(path3a_value)
aload.write(path3a1)
and surprisely got a continuous line rather than seperated lines
100110,P,-1125.0100113,P,-1125.0100114,P,-1125.0100120,P,-1125.0100121,P,-1125.0100122,P,-1125.0100124...

What kind of mistakes had I made? I am thinking of it and cannot work it out.

best wishes

ning

Hi, Guys

I sorted it out by adding
path3a1=','.join(path3a_value)+'\n'

However, i still have no idea about why this strange format appears. I guess it is related to length of float somehow.

all the best

ning

When you split by commas you leave in the newline characters if there are any. So you the third entry in the first line isn't really '-4500', it is '-4500\n'. In the remainder of your program you must be stripping off the '\n' somehow. If you didn't then the float conversion that you have in your code would give an error.

Hi. Nine Tails,

I checked it out and found that stripping \n generates a more strange result. Additionaly, I got another similar problem. When I use this pattern below to write a text file,

cload=open('cload.txt','w')
cload.write(path3b1)
cload.close()

The obtained clload.txt has an empty line at the end of this file. Where does it come from? How can I can delete it?

Thank you so much.

Ning

Hi, there

Actaully my final question is irrelevant to my original question.

Thank you.

ning

Be a part of the DaniWeb community

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