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?










share|improve this question
























  • 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










  • 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















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?










share|improve this question
























  • 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










  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 16:29

























asked Nov 12 at 16:20









dratoms

195




195












  • 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










  • 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










  • 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












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:




  1. Change the regex to r'Block2:s*(.*)' (I added s*).
    This way you avoid keeping initial space(s) from each Block2, if any.


  2. It is not clear, why you use secondlist.
    Maybe you should add the strings matched directly to list1.







share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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:




  1. Change the regex to r'Block2:s*(.*)' (I added s*).
    This way you avoid keeping initial space(s) from each Block2, if any.


  2. It is not clear, why you use secondlist.
    Maybe you should add the strings matched directly to list1.







share|improve this answer























  • 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















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:




  1. Change the regex to r'Block2:s*(.*)' (I added s*).
    This way you avoid keeping initial space(s) from each Block2, if any.


  2. It is not clear, why you use secondlist.
    Maybe you should add the strings matched directly to list1.







share|improve this answer























  • 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













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:




  1. Change the regex to r'Block2:s*(.*)' (I added s*).
    This way you avoid keeping initial space(s) from each Block2, if any.


  2. It is not clear, why you use secondlist.
    Maybe you should add the strings matched directly to list1.







share|improve this answer














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:




  1. Change the regex to r'Block2:s*(.*)' (I added s*).
    This way you avoid keeping initial space(s) from each Block2, if any.


  2. It is not clear, why you use secondlist.
    Maybe you should add the strings matched directly to list1.








share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?