Python list of lists with strings with regex
up vote
-1
down vote
favorite
segmented
is a list of lists. Each element could be list of one or more string elements. And each string element is of the form:
Block1: some strings
Block2: some strings
Now not all string elements need to have Block2
.
I want to run a loop where each element in 'segmented' is selected and then further loops with the secondary elements and tests for Block2
. If present the string after Block2
is selected. IF there are more than 1 secondary elements then all the string is joined to a single string. The code I tried is as below
blk2 = re.compile(r'Block2:(.*)', re.DOTALL)
list1 =
for i in segmented:
secondlist =
for j in i:
if re.search(blk2, j).group(1) is not None: # testing if Block2 exists
txt = re.search(blk2, j).group(1) # picking up strings after Block2.
secondlist.append(txt) #appending the string to an intermediate list
'n'.join(secondlist) #if there are more than 1 element in secondlist joining them to one element
list1.append(secondlist[0]) # appending the joined element to list1
I keep getting NoneType
errors. What am I doing wrong?
python regex list
|
show 1 more comment
up vote
-1
down vote
favorite
segmented
is a list of lists. Each element could be list of one or more string elements. And each string element is of the form:
Block1: some strings
Block2: some strings
Now not all string elements need to have Block2
.
I want to run a loop where each element in 'segmented' is selected and then further loops with the secondary elements and tests for Block2
. If present the string after Block2
is selected. IF there are more than 1 secondary elements then all the string is joined to a single string. The code I tried is as below
blk2 = re.compile(r'Block2:(.*)', re.DOTALL)
list1 =
for i in segmented:
secondlist =
for j in i:
if re.search(blk2, j).group(1) is not None: # testing if Block2 exists
txt = re.search(blk2, j).group(1) # picking up strings after Block2.
secondlist.append(txt) #appending the string to an intermediate list
'n'.join(secondlist) #if there are more than 1 element in secondlist joining them to one element
list1.append(secondlist[0]) # appending the joined element to list1
I keep getting NoneType
errors. What am I doing wrong?
python regex list
secondlist.append(txt)
: buttxt
can beNone
if regex doesn't match
– Jean-François Fabre
Nov 12 at 16:22
if there is block2 in an element there is always some text after it. So the regex can't return None. But some of the elements may not have Block2 at all which I hoped would be eliminated by if re.search(blk2, j).group(1) is not None:
– dratoms
Nov 12 at 16:28
don't perform the search twice. Store the search in a variable... and provide traceback of the error, don't just describe it
– Jean-François Fabre
Nov 12 at 16:29
BTW'n'.join(secondlist)
does nothing useful since you don't store the result...
– Jean-François Fabre
Nov 12 at 16:29
so 'n'.join(secondlist) should be stored as another variable?
– dratoms
Nov 12 at 16:32
|
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
segmented
is a list of lists. Each element could be list of one or more string elements. And each string element is of the form:
Block1: some strings
Block2: some strings
Now not all string elements need to have Block2
.
I want to run a loop where each element in 'segmented' is selected and then further loops with the secondary elements and tests for Block2
. If present the string after Block2
is selected. IF there are more than 1 secondary elements then all the string is joined to a single string. The code I tried is as below
blk2 = re.compile(r'Block2:(.*)', re.DOTALL)
list1 =
for i in segmented:
secondlist =
for j in i:
if re.search(blk2, j).group(1) is not None: # testing if Block2 exists
txt = re.search(blk2, j).group(1) # picking up strings after Block2.
secondlist.append(txt) #appending the string to an intermediate list
'n'.join(secondlist) #if there are more than 1 element in secondlist joining them to one element
list1.append(secondlist[0]) # appending the joined element to list1
I keep getting NoneType
errors. What am I doing wrong?
python regex list
segmented
is a list of lists. Each element could be list of one or more string elements. And each string element is of the form:
Block1: some strings
Block2: some strings
Now not all string elements need to have Block2
.
I want to run a loop where each element in 'segmented' is selected and then further loops with the secondary elements and tests for Block2
. If present the string after Block2
is selected. IF there are more than 1 secondary elements then all the string is joined to a single string. The code I tried is as below
blk2 = re.compile(r'Block2:(.*)', re.DOTALL)
list1 =
for i in segmented:
secondlist =
for j in i:
if re.search(blk2, j).group(1) is not None: # testing if Block2 exists
txt = re.search(blk2, j).group(1) # picking up strings after Block2.
secondlist.append(txt) #appending the string to an intermediate list
'n'.join(secondlist) #if there are more than 1 element in secondlist joining them to one element
list1.append(secondlist[0]) # appending the joined element to list1
I keep getting NoneType
errors. What am I doing wrong?
python regex list
python regex list
edited Nov 12 at 16:29
asked Nov 12 at 16:20
dratoms
195
195
secondlist.append(txt)
: buttxt
can beNone
if regex doesn't match
– Jean-François Fabre
Nov 12 at 16:22
if there is block2 in an element there is always some text after it. So the regex can't return None. But some of the elements may not have Block2 at all which I hoped would be eliminated by if re.search(blk2, j).group(1) is not None:
– dratoms
Nov 12 at 16:28
don't perform the search twice. Store the search in a variable... and provide traceback of the error, don't just describe it
– Jean-François Fabre
Nov 12 at 16:29
BTW'n'.join(secondlist)
does nothing useful since you don't store the result...
– Jean-François Fabre
Nov 12 at 16:29
so 'n'.join(secondlist) should be stored as another variable?
– dratoms
Nov 12 at 16:32
|
show 1 more comment
secondlist.append(txt)
: buttxt
can beNone
if regex doesn't match
– Jean-François Fabre
Nov 12 at 16:22
if there is block2 in an element there is always some text after it. So the regex can't return None. But some of the elements may not have Block2 at all which I hoped would be eliminated by if re.search(blk2, j).group(1) is not None:
– dratoms
Nov 12 at 16:28
don't perform the search twice. Store the search in a variable... and provide traceback of the error, don't just describe it
– Jean-François Fabre
Nov 12 at 16:29
BTW'n'.join(secondlist)
does nothing useful since you don't store the result...
– Jean-François Fabre
Nov 12 at 16:29
so 'n'.join(secondlist) should be stored as another variable?
– dratoms
Nov 12 at 16:32
secondlist.append(txt)
: but txt
can be None
if regex doesn't match– Jean-François Fabre
Nov 12 at 16:22
secondlist.append(txt)
: but txt
can be None
if regex doesn't match– Jean-François Fabre
Nov 12 at 16:22
if there is block2 in an element there is always some text after it. So the regex can't return None. But some of the elements may not have Block2 at all which I hoped would be eliminated by if re.search(blk2, j).group(1) is not None:
– dratoms
Nov 12 at 16:28
if there is block2 in an element there is always some text after it. So the regex can't return None. But some of the elements may not have Block2 at all which I hoped would be eliminated by if re.search(blk2, j).group(1) is not None:
– dratoms
Nov 12 at 16:28
don't perform the search twice. Store the search in a variable... and provide traceback of the error, don't just describe it
– Jean-François Fabre
Nov 12 at 16:29
don't perform the search twice. Store the search in a variable... and provide traceback of the error, don't just describe it
– Jean-François Fabre
Nov 12 at 16:29
BTW
'n'.join(secondlist)
does nothing useful since you don't store the result...– Jean-François Fabre
Nov 12 at 16:29
BTW
'n'.join(secondlist)
does nothing useful since you don't store the result...– Jean-François Fabre
Nov 12 at 16:29
so 'n'.join(secondlist) should be stored as another variable?
– dratoms
Nov 12 at 16:32
so 'n'.join(secondlist) should be stored as another variable?
– dratoms
Nov 12 at 16:32
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
The source of your problem is row:
if re.search(blk2, j).group(1) is not None:
This code fails when j
contains only Block1
(no Block2
).
In such case search
does not match anything, so the result of search
is None
.
Then, on None
object (of type NoneType
), you attempt to call group(1)
which raises exception AttributeError: 'NoneType' object has no attribute 'group'
.
Another remark: There is no need to call search
twice, as you did.
So change this fragment to:
m = re.search(blk2, j)
if m:
txt = m.group(1)
Additional hints:
Change the regex to
r'Block2:s*(.*)'
(I addeds*
).
This way you avoid keeping initial space(s) from eachBlock2
, if any.It is not clear, why you use
secondlist
.
Maybe you should add the strings matched directly tolist1
.
thanks Valdi_Bo. I though that re.search().group() would be evaluated to None if Block2 is not present. Your explanation has made it clear where I went wrong and thanks to you I have rectified my code.
– dratoms
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The source of your problem is row:
if re.search(blk2, j).group(1) is not None:
This code fails when j
contains only Block1
(no Block2
).
In such case search
does not match anything, so the result of search
is None
.
Then, on None
object (of type NoneType
), you attempt to call group(1)
which raises exception AttributeError: 'NoneType' object has no attribute 'group'
.
Another remark: There is no need to call search
twice, as you did.
So change this fragment to:
m = re.search(blk2, j)
if m:
txt = m.group(1)
Additional hints:
Change the regex to
r'Block2:s*(.*)'
(I addeds*
).
This way you avoid keeping initial space(s) from eachBlock2
, if any.It is not clear, why you use
secondlist
.
Maybe you should add the strings matched directly tolist1
.
thanks Valdi_Bo. I though that re.search().group() would be evaluated to None if Block2 is not present. Your explanation has made it clear where I went wrong and thanks to you I have rectified my code.
– dratoms
2 days ago
add a comment |
up vote
0
down vote
The source of your problem is row:
if re.search(blk2, j).group(1) is not None:
This code fails when j
contains only Block1
(no Block2
).
In such case search
does not match anything, so the result of search
is None
.
Then, on None
object (of type NoneType
), you attempt to call group(1)
which raises exception AttributeError: 'NoneType' object has no attribute 'group'
.
Another remark: There is no need to call search
twice, as you did.
So change this fragment to:
m = re.search(blk2, j)
if m:
txt = m.group(1)
Additional hints:
Change the regex to
r'Block2:s*(.*)'
(I addeds*
).
This way you avoid keeping initial space(s) from eachBlock2
, if any.It is not clear, why you use
secondlist
.
Maybe you should add the strings matched directly tolist1
.
thanks Valdi_Bo. I though that re.search().group() would be evaluated to None if Block2 is not present. Your explanation has made it clear where I went wrong and thanks to you I have rectified my code.
– dratoms
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
The source of your problem is row:
if re.search(blk2, j).group(1) is not None:
This code fails when j
contains only Block1
(no Block2
).
In such case search
does not match anything, so the result of search
is None
.
Then, on None
object (of type NoneType
), you attempt to call group(1)
which raises exception AttributeError: 'NoneType' object has no attribute 'group'
.
Another remark: There is no need to call search
twice, as you did.
So change this fragment to:
m = re.search(blk2, j)
if m:
txt = m.group(1)
Additional hints:
Change the regex to
r'Block2:s*(.*)'
(I addeds*
).
This way you avoid keeping initial space(s) from eachBlock2
, if any.It is not clear, why you use
secondlist
.
Maybe you should add the strings matched directly tolist1
.
The source of your problem is row:
if re.search(blk2, j).group(1) is not None:
This code fails when j
contains only Block1
(no Block2
).
In such case search
does not match anything, so the result of search
is None
.
Then, on None
object (of type NoneType
), you attempt to call group(1)
which raises exception AttributeError: 'NoneType' object has no attribute 'group'
.
Another remark: There is no need to call search
twice, as you did.
So change this fragment to:
m = re.search(blk2, j)
if m:
txt = m.group(1)
Additional hints:
Change the regex to
r'Block2:s*(.*)'
(I addeds*
).
This way you avoid keeping initial space(s) from eachBlock2
, if any.It is not clear, why you use
secondlist
.
Maybe you should add the strings matched directly tolist1
.
edited Nov 12 at 18:31
answered Nov 12 at 18:14
Valdi_Bo
4,3302614
4,3302614
thanks Valdi_Bo. I though that re.search().group() would be evaluated to None if Block2 is not present. Your explanation has made it clear where I went wrong and thanks to you I have rectified my code.
– dratoms
2 days ago
add a comment |
thanks Valdi_Bo. I though that re.search().group() would be evaluated to None if Block2 is not present. Your explanation has made it clear where I went wrong and thanks to you I have rectified my code.
– dratoms
2 days ago
thanks Valdi_Bo. I though that re.search().group() would be evaluated to None if Block2 is not present. Your explanation has made it clear where I went wrong and thanks to you I have rectified my code.
– dratoms
2 days ago
thanks Valdi_Bo. I though that re.search().group() would be evaluated to None if Block2 is not present. Your explanation has made it clear where I went wrong and thanks to you I have rectified my code.
– dratoms
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266177%2fpython-list-of-lists-with-strings-with-regex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
secondlist.append(txt)
: buttxt
can beNone
if regex doesn't match– Jean-François Fabre
Nov 12 at 16:22
if there is block2 in an element there is always some text after it. So the regex can't return None. But some of the elements may not have Block2 at all which I hoped would be eliminated by if re.search(blk2, j).group(1) is not None:
– dratoms
Nov 12 at 16:28
don't perform the search twice. Store the search in a variable... and provide traceback of the error, don't just describe it
– Jean-François Fabre
Nov 12 at 16:29
BTW
'n'.join(secondlist)
does nothing useful since you don't store the result...– Jean-François Fabre
Nov 12 at 16:29
so 'n'.join(secondlist) should be stored as another variable?
– dratoms
Nov 12 at 16:32