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.
-
2Welcome to Stack Overflow. Please read the help pages, take the SO tour, read How to Ask, as well as this question checklist. Lastly please learn how to create a minimal reproducible example 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– Some programmer dude2022-07-21 06:54:10 +00:00Commented Jul 21, 2022 at 6:54
-
Does this answer your question? copy section of text in file pythonsabsa– sabsa2022-07-21 06:59:09 +00:00Commented Jul 21, 2022 at 6: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 fileKroons– Kroons2022-07-21 07:04:34 +00:00Commented Jul 21, 2022 at 7: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– Community Bot2022-07-21 08:02:48 +00:00Commented Jul 21, 2022 at 8:02
Add a comment
|
1 Answer
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.
1 Comment
Kroons
Absolute life saver thank you!