-1

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.

4
  • 2
    Welcome 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. Commented Jul 21, 2022 at 6:54
  • Does this answer your question? copy section of text in file python Commented 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 file Commented 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. Commented Jul 21, 2022 at 8:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Absolute life saver thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.