joining output from regex search
- I have a regex that looks for numbers in a file.
- I put results in a list
The problem is that it prints each results on a new line for every single number it finds. it aslo ignore the list I've created.
What I want to do is to have all the numbers into one list.
I used join() but it doesn't works.
code :
def readfile():
regex = re.compile('d+')
for num in regex.findall(open('/path/to/file').read()):
lst = [num]
jn = ''.join(lst)
print(jn)
output :
122
34
764
python python-3.x
add a comment |
- I have a regex that looks for numbers in a file.
- I put results in a list
The problem is that it prints each results on a new line for every single number it finds. it aslo ignore the list I've created.
What I want to do is to have all the numbers into one list.
I used join() but it doesn't works.
code :
def readfile():
regex = re.compile('d+')
for num in regex.findall(open('/path/to/file').read()):
lst = [num]
jn = ''.join(lst)
print(jn)
output :
122
34
764
python python-3.x
add a comment |
- I have a regex that looks for numbers in a file.
- I put results in a list
The problem is that it prints each results on a new line for every single number it finds. it aslo ignore the list I've created.
What I want to do is to have all the numbers into one list.
I used join() but it doesn't works.
code :
def readfile():
regex = re.compile('d+')
for num in regex.findall(open('/path/to/file').read()):
lst = [num]
jn = ''.join(lst)
print(jn)
output :
122
34
764
python python-3.x
- I have a regex that looks for numbers in a file.
- I put results in a list
The problem is that it prints each results on a new line for every single number it finds. it aslo ignore the list I've created.
What I want to do is to have all the numbers into one list.
I used join() but it doesn't works.
code :
def readfile():
regex = re.compile('d+')
for num in regex.findall(open('/path/to/file').read()):
lst = [num]
jn = ''.join(lst)
print(jn)
output :
122
34
764
python python-3.x
python python-3.x
edited Jan 1 at 10:59
Patrick Artner
24.6k62443
24.6k62443
asked Jan 1 at 10:40
shimoshimo
283
283
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
In your case, regex.findall()
returns a list and you are are joining in each iteration and printing it.
That is why you're seeing this problem.
You can try something like this.
numbers.txt
Xy10Ab
Tiger20
Beta30Man
56
My45one
statements:
>>> import re
>>>
>>> regex = re.compile(r'd+')
>>> lst =
>>>
>>> for num in regex.findall(open('numbers.txt').read()):
... lst.append(num)
...
>>> lst
['10', '20', '30', '56', '45']
>>>
>>> jn = ''.join(lst)
>>>
>>> jn
'1020305645'
>>>
>>> jn2 = 'n'.join(lst)
>>> jn2
'10n20n30n56n45'
>>>
>>> print(jn2)
10
20
30
56
45
>>>
>>> nums = [int(n) for n in lst]
>>> nums
[10, 20, 30, 56, 45]
>>>
>>> sum(nums)
161
>>>
add a comment |
What goes wrong:
# this iterates the single numbers you find - one by one
for num in regex.findall(open('/path/to/file').read()):
lst = [num] # this puts one number back into a new list
jn = ''.join(lst) # this gets the number back out of the new list
print(jn) # this prints one number
Fixing it:
Reading re.findall() show's you, it returns a list already.
There is no(t much) need to use a for
on it to print it.
If you want a list - simply use re.findall()
's return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):
import re
my_r = re.compile(r'd+') # define pattern as raw-string
numbers = my_r.findall("123 456 789") # get the list
print(numbers)
# different methods to print a list on one line
# adjust sep / end to fit your needs
print( *numbers, sep=", ") # print #1
for n in numbers[:-1]: # print #2
print(n, end = ", ")
print(numbers[-1])
print(', '.join(numbers)) # print #3
Output:
['123', '456', '789'] # list of found strings that are numbers
123, 456, 789
123, 456, 789
123, 456, 789
Doku:
- print() function for
sep=
andend=
- Printing an int list in a single line python3
Convert all strings in a list to int ... if you need the list as numbers
More on printing in one line:
- Print in one line dynamically
- Python: multiple prints on the same line
- How to print without newline or space?
- Print new output on same line
add a comment |
Use list built-in functions to append new values.
def readfile():
regex = re.compile('d+')
lst =
for num in regex.findall(open('/path/to/file').read()):
lst.append(num)
print(lst)
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53994798%2fjoining-output-from-regex-search%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your case, regex.findall()
returns a list and you are are joining in each iteration and printing it.
That is why you're seeing this problem.
You can try something like this.
numbers.txt
Xy10Ab
Tiger20
Beta30Man
56
My45one
statements:
>>> import re
>>>
>>> regex = re.compile(r'd+')
>>> lst =
>>>
>>> for num in regex.findall(open('numbers.txt').read()):
... lst.append(num)
...
>>> lst
['10', '20', '30', '56', '45']
>>>
>>> jn = ''.join(lst)
>>>
>>> jn
'1020305645'
>>>
>>> jn2 = 'n'.join(lst)
>>> jn2
'10n20n30n56n45'
>>>
>>> print(jn2)
10
20
30
56
45
>>>
>>> nums = [int(n) for n in lst]
>>> nums
[10, 20, 30, 56, 45]
>>>
>>> sum(nums)
161
>>>
add a comment |
In your case, regex.findall()
returns a list and you are are joining in each iteration and printing it.
That is why you're seeing this problem.
You can try something like this.
numbers.txt
Xy10Ab
Tiger20
Beta30Man
56
My45one
statements:
>>> import re
>>>
>>> regex = re.compile(r'd+')
>>> lst =
>>>
>>> for num in regex.findall(open('numbers.txt').read()):
... lst.append(num)
...
>>> lst
['10', '20', '30', '56', '45']
>>>
>>> jn = ''.join(lst)
>>>
>>> jn
'1020305645'
>>>
>>> jn2 = 'n'.join(lst)
>>> jn2
'10n20n30n56n45'
>>>
>>> print(jn2)
10
20
30
56
45
>>>
>>> nums = [int(n) for n in lst]
>>> nums
[10, 20, 30, 56, 45]
>>>
>>> sum(nums)
161
>>>
add a comment |
In your case, regex.findall()
returns a list and you are are joining in each iteration and printing it.
That is why you're seeing this problem.
You can try something like this.
numbers.txt
Xy10Ab
Tiger20
Beta30Man
56
My45one
statements:
>>> import re
>>>
>>> regex = re.compile(r'd+')
>>> lst =
>>>
>>> for num in regex.findall(open('numbers.txt').read()):
... lst.append(num)
...
>>> lst
['10', '20', '30', '56', '45']
>>>
>>> jn = ''.join(lst)
>>>
>>> jn
'1020305645'
>>>
>>> jn2 = 'n'.join(lst)
>>> jn2
'10n20n30n56n45'
>>>
>>> print(jn2)
10
20
30
56
45
>>>
>>> nums = [int(n) for n in lst]
>>> nums
[10, 20, 30, 56, 45]
>>>
>>> sum(nums)
161
>>>
In your case, regex.findall()
returns a list and you are are joining in each iteration and printing it.
That is why you're seeing this problem.
You can try something like this.
numbers.txt
Xy10Ab
Tiger20
Beta30Man
56
My45one
statements:
>>> import re
>>>
>>> regex = re.compile(r'd+')
>>> lst =
>>>
>>> for num in regex.findall(open('numbers.txt').read()):
... lst.append(num)
...
>>> lst
['10', '20', '30', '56', '45']
>>>
>>> jn = ''.join(lst)
>>>
>>> jn
'1020305645'
>>>
>>> jn2 = 'n'.join(lst)
>>> jn2
'10n20n30n56n45'
>>>
>>> print(jn2)
10
20
30
56
45
>>>
>>> nums = [int(n) for n in lst]
>>> nums
[10, 20, 30, 56, 45]
>>>
>>> sum(nums)
161
>>>
edited Jan 1 at 11:42
answered Jan 1 at 11:26
hygullhygull
3,67021431
3,67021431
add a comment |
add a comment |
What goes wrong:
# this iterates the single numbers you find - one by one
for num in regex.findall(open('/path/to/file').read()):
lst = [num] # this puts one number back into a new list
jn = ''.join(lst) # this gets the number back out of the new list
print(jn) # this prints one number
Fixing it:
Reading re.findall() show's you, it returns a list already.
There is no(t much) need to use a for
on it to print it.
If you want a list - simply use re.findall()
's return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):
import re
my_r = re.compile(r'd+') # define pattern as raw-string
numbers = my_r.findall("123 456 789") # get the list
print(numbers)
# different methods to print a list on one line
# adjust sep / end to fit your needs
print( *numbers, sep=", ") # print #1
for n in numbers[:-1]: # print #2
print(n, end = ", ")
print(numbers[-1])
print(', '.join(numbers)) # print #3
Output:
['123', '456', '789'] # list of found strings that are numbers
123, 456, 789
123, 456, 789
123, 456, 789
Doku:
- print() function for
sep=
andend=
- Printing an int list in a single line python3
Convert all strings in a list to int ... if you need the list as numbers
More on printing in one line:
- Print in one line dynamically
- Python: multiple prints on the same line
- How to print without newline or space?
- Print new output on same line
add a comment |
What goes wrong:
# this iterates the single numbers you find - one by one
for num in regex.findall(open('/path/to/file').read()):
lst = [num] # this puts one number back into a new list
jn = ''.join(lst) # this gets the number back out of the new list
print(jn) # this prints one number
Fixing it:
Reading re.findall() show's you, it returns a list already.
There is no(t much) need to use a for
on it to print it.
If you want a list - simply use re.findall()
's return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):
import re
my_r = re.compile(r'd+') # define pattern as raw-string
numbers = my_r.findall("123 456 789") # get the list
print(numbers)
# different methods to print a list on one line
# adjust sep / end to fit your needs
print( *numbers, sep=", ") # print #1
for n in numbers[:-1]: # print #2
print(n, end = ", ")
print(numbers[-1])
print(', '.join(numbers)) # print #3
Output:
['123', '456', '789'] # list of found strings that are numbers
123, 456, 789
123, 456, 789
123, 456, 789
Doku:
- print() function for
sep=
andend=
- Printing an int list in a single line python3
Convert all strings in a list to int ... if you need the list as numbers
More on printing in one line:
- Print in one line dynamically
- Python: multiple prints on the same line
- How to print without newline or space?
- Print new output on same line
add a comment |
What goes wrong:
# this iterates the single numbers you find - one by one
for num in regex.findall(open('/path/to/file').read()):
lst = [num] # this puts one number back into a new list
jn = ''.join(lst) # this gets the number back out of the new list
print(jn) # this prints one number
Fixing it:
Reading re.findall() show's you, it returns a list already.
There is no(t much) need to use a for
on it to print it.
If you want a list - simply use re.findall()
's return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):
import re
my_r = re.compile(r'd+') # define pattern as raw-string
numbers = my_r.findall("123 456 789") # get the list
print(numbers)
# different methods to print a list on one line
# adjust sep / end to fit your needs
print( *numbers, sep=", ") # print #1
for n in numbers[:-1]: # print #2
print(n, end = ", ")
print(numbers[-1])
print(', '.join(numbers)) # print #3
Output:
['123', '456', '789'] # list of found strings that are numbers
123, 456, 789
123, 456, 789
123, 456, 789
Doku:
- print() function for
sep=
andend=
- Printing an int list in a single line python3
Convert all strings in a list to int ... if you need the list as numbers
More on printing in one line:
- Print in one line dynamically
- Python: multiple prints on the same line
- How to print without newline or space?
- Print new output on same line
What goes wrong:
# this iterates the single numbers you find - one by one
for num in regex.findall(open('/path/to/file').read()):
lst = [num] # this puts one number back into a new list
jn = ''.join(lst) # this gets the number back out of the new list
print(jn) # this prints one number
Fixing it:
Reading re.findall() show's you, it returns a list already.
There is no(t much) need to use a for
on it to print it.
If you want a list - simply use re.findall()
's return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):
import re
my_r = re.compile(r'd+') # define pattern as raw-string
numbers = my_r.findall("123 456 789") # get the list
print(numbers)
# different methods to print a list on one line
# adjust sep / end to fit your needs
print( *numbers, sep=", ") # print #1
for n in numbers[:-1]: # print #2
print(n, end = ", ")
print(numbers[-1])
print(', '.join(numbers)) # print #3
Output:
['123', '456', '789'] # list of found strings that are numbers
123, 456, 789
123, 456, 789
123, 456, 789
Doku:
- print() function for
sep=
andend=
- Printing an int list in a single line python3
Convert all strings in a list to int ... if you need the list as numbers
More on printing in one line:
- Print in one line dynamically
- Python: multiple prints on the same line
- How to print without newline or space?
- Print new output on same line
edited Jan 1 at 11:23
answered Jan 1 at 11:09
Patrick ArtnerPatrick Artner
24.6k62443
24.6k62443
add a comment |
add a comment |
Use list built-in functions to append new values.
def readfile():
regex = re.compile('d+')
lst =
for num in regex.findall(open('/path/to/file').read()):
lst.append(num)
print(lst)
add a comment |
Use list built-in functions to append new values.
def readfile():
regex = re.compile('d+')
lst =
for num in regex.findall(open('/path/to/file').read()):
lst.append(num)
print(lst)
add a comment |
Use list built-in functions to append new values.
def readfile():
regex = re.compile('d+')
lst =
for num in regex.findall(open('/path/to/file').read()):
lst.append(num)
print(lst)
Use list built-in functions to append new values.
def readfile():
regex = re.compile('d+')
lst =
for num in regex.findall(open('/path/to/file').read()):
lst.append(num)
print(lst)
answered Jan 1 at 11:02
ThunderMindThunderMind
615113
615113
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53994798%2fjoining-output-from-regex-search%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