I have a txt file full of random stuff I do not want an a few hundred 18 digit numbers that I want how would I copy those numbers without copying the rest of it.
Asked
Active
Viewed 51 times
-1
-
2Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mre] of *your own* attempt, and [edit] your question to show it together with a description of the problems you have with it. – Some programmer dude Jul 21 '22 at 06:54
-
Does this answer your question? [copy section of text in file python](https://stackoverflow.com/questions/35758444/copy-section-of-text-in-file-python) – Sabsa Jul 21 '22 at 06:59
-
No im looking for a way to take a number above or below a certain value copy than and paste it into a txt file – Kroons Jul 21 '22 at 07:04
-
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 21 '22 at 08:02
1 Answers
0
Regex might be a good solution here:
import re
pattern = re.compile(r"\d{18}")
with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
for line in infile:
my_digits = pattern.findall(line)
if my_digits:
for digit in my_digits:
outfile.writelines(digit + '\n')
\d{18} looks for digits of 18 length (you can also do ranges: \d{x,y}). Than you go through your file line by line, grab the digits from each line into a list and write out the found digits if there is any.

mmarton
- 49
- 3