Hi, there

Asssuming that I have some lines below

28 J=5202,5204,7497,7498 SEC=WALL1
1185 J=289,3875,2673 SEC=SLAB2

I would like to count number of digital after "J=" on the above two lines in a text file. I am expecting results below

the first line: 5202,5204,7497,7498 =====> Answer: 4
the second line: 289,3875,2673 =====> Answer: 3

Thank you so much

Best Wishes

Ning

I had the answer for this an hour ago but suddenly the site stopped responding. For this problem you have to split the string into bits and the count the amount of bits.. that sounds really bad.. here is some code that demonstrates this

def Digit(line):
    line = line[line.rindex("J")+1:]
    line = line.split()[0]
    line = line.split(',')
    return len(line)

Digit("28 J=5202,5204,7497,7498 SEC=WALL1")
#output 3

What this does is it first takes off all the line after the = sign and makes line equal that then we split it by the spaces and get the first bit of that. Then we are only left with the "234,14,21" bit (i just made up those numbers). Then we just split it by the comma and count the amount of things in the list and return that value. It should word every time. If it dosent it should be just a simple coding error, i havent actually tested the code :P

You could as well use a regex like this

import re
pattern = re.compile("J=(\d+)")
def Digit(line):
    match = pattern.search(line)
    if match is None:
        return 0
    else:
        return len(match.group(1))

print (Digit("28 J=5202,5204,7497,7498 SEC=WALL1"))
print (Digit("1185 J=289,3875,2673 SEC=SLAB2"))

"""
my output --->
4
3

You could as well use a regex like this

import re
pattern = re.compile("J=(\d+)")
def Digit(line):
    match = pattern.search(line)
    if match is None:
        return 0
    else:
        return len(match.group(1))

print (Digit("28 J=5202,5204,7497,7498 SEC=WALL1"))
print (Digit("1185 J=289,3875,2673 SEC=SLAB2"))

"""
my output --->
4
3

Hi, Gribouillis

Thank you so much. I tried your code. It does not work out for many lines below:
61 J=5602,5601,7528,7527 SEC=WALL1
62 J=5601,5600,7527,7526 SEC=WALL1
63 J=5600,5599,7526,7525 SEC=WALL1
64 J=5599,5598,7525,7524 SEC=WALL1
65 J=5598,5597,7524,7523 SEC=WALL1
66 J=5597,5596,7523,7522 SEC=WALL1
67 J=5596,106,7522,203 SEC=WALL1
68 J=319,628,629 SEC=SLAB1
69 J=1204,359,4 SEC=SLAB1
70 J=337,1205,6,1200 SEC=SLAB1
71 J=1213,337,1207,6 SEC=SLAB1
72 J=294,1221,10,1217 SEC=SLAB1
73 J=1222,314,1215,8 SEC=SLAB1
74 J=631,339,1249,1248 SEC=SLAB1

I also put my relevant code here
def Digit(pattern,line): #### I add one parameter here
match = pattern.search(line)
if match is None:
return 0
else:
return len(match.group(1))

#######I use this part to seperate data into different files
####### according to how many digital numbers one has
if Digit(p3,line1) == 4:
s4inf.write(mod_path4a)
if node_num(p3,line1) == 3:
s3inf.write(mod_path4a)

Eventually the code only work for one line, I am study the reason why it failed. Do you have any clue about it?

all the best

I had the answer for this an hour ago but suddenly the site stopped responding. For this problem you have to split the string into bits and the count the amount of bits.. that sounds really bad.. here is some code that demonstrates this

def Digit(line):
    line = line[line.rindex("J")+1:]
    line = line.split()[0]
    line = line.split(',')
    return len(line)

Digit("28 J=5202,5204,7497,7498 SEC=WALL1")
#output 3

What this does is it first takes off all the line after the = sign and makes line equal that then we split it by the spaces and get the first bit of that. Then we are only left with the "234,14,21" bit (i just made up those numbers). Then we just split it by the comma and count the amount of things in the list and return that value. It should word every time. If it dosent it should be just a simple coding error, i havent actually tested the code :P

Hi, paulthom12345

Thank you so much. Your code works very well and sorts out my problem.

all the best

ning

This question has already been answered. Start a new discussion instead.