Skip to main content
added 622 characters in body
Source Link
Totem
  • 7.4k
  • 5
  • 43
  • 67
with open("numberlist.txt") as f: # this auto closes the file after reading. It's good practice
    numbers = f.readlines() # numbers is a list of all the numbers(a list of lines in the file)

if there are unwanted spaces in the lines(or just in case there are):

numbers = [n.strip() for n in numbers] # takes out unwanted spaces on the ends

and if you find there are commas or something after the numbers, you can do this:

numbers = [n[:-1] for n in numbers] # slices off the last character of each line/list item
    
for number in numbers:
    #do whatever you want here

EDIT:

Alternatively you could use a regular expression, and commas and spaces won't matter:

import re

n = ['1993-06-11 5570',
     '930611-5570',
     '930611 5570',
     '93 05115570',
     '1993 05 11 55 70',
     '1993 05 11 5570']

regex = '([0-9]+(?:[- ]?[0-9]+)*)'
match_nums = [re.search(regex, num) for num in n]
results = [i.groups() for i in match_nums]
for i in results:
    print i

('1993-06-11 5570',)
('930611-5570',)
('930611 5570',)
('93 05115570',)
('1993 05 11 55 70',)
('1993 05 11 5570',)

for info on regular expressions, see here

with open("numberlist.txt") as f: # this auto closes the file after reading. It's good practice
    numbers = f.readlines() # numbers is a list of all the numbers(a list of lines in the file)

if there are unwanted spaces in the lines(or just in case there are):

numbers = [n.strip() for n in numbers] # takes out unwanted spaces on the ends

and if you find there are commas or something after the numbers, you can do this:

numbers = [n[:-1] for n in numbers] # slices off the last character of each line/list item
    
for number in numbers:
    #do whatever you want here
with open("numberlist.txt") as f: # this auto closes the file after reading. It's good practice
    numbers = f.readlines() # numbers is a list of all the numbers(a list of lines in the file)

if there are unwanted spaces in the lines(or just in case there are):

numbers = [n.strip() for n in numbers] # takes out unwanted spaces on the ends

and if you find there are commas or something after the numbers, you can do this:

numbers = [n[:-1] for n in numbers] # slices off the last character of each line/list item
    
for number in numbers:
    #do whatever you want here

EDIT:

Alternatively you could use a regular expression, and commas and spaces won't matter:

import re

n = ['1993-06-11 5570',
     '930611-5570',
     '930611 5570',
     '93 05115570',
     '1993 05 11 55 70',
     '1993 05 11 5570']

regex = '([0-9]+(?:[- ]?[0-9]+)*)'
match_nums = [re.search(regex, num) for num in n]
results = [i.groups() for i in match_nums]
for i in results:
    print i

('1993-06-11 5570',)
('930611-5570',)
('930611 5570',)
('93 05115570',)
('1993 05 11 55 70',)
('1993 05 11 5570',)

for info on regular expressions, see here

added 287 characters in body
Source Link
Totem
  • 7.4k
  • 5
  • 43
  • 67
with open("numberlist.txt") as f: # this auto closes the file after reading. It's good practice
    numbers = f.readlines() # numbers is a list of all the numbers(a list of lines in the file)

if there are unwanted spaces in the lines(or just in case there are):

numbers = [n.strip() for n in numbers] # takes out unwanted spaces on the ends

and if you find there are commas or something after the numbers, you can do this:

numbers = [n[:-1] for n in numbers] # slices off the last character of each line/list item
    
for number in numbers:
    #do whatever you want here
with open("numberlist.txt") as f: # this auto closes the file after reading. It's good practice
    numbers = f.readlines() # numbers is a list of all the numbers(a list of lines in the file)
    
for number in numbers:
    #do whatever you want here
with open("numberlist.txt") as f: # this auto closes the file after reading. It's good practice
    numbers = f.readlines() # numbers is a list of all the numbers(a list of lines in the file)

if there are unwanted spaces in the lines(or just in case there are):

numbers = [n.strip() for n in numbers] # takes out unwanted spaces on the ends

and if you find there are commas or something after the numbers, you can do this:

numbers = [n[:-1] for n in numbers] # slices off the last character of each line/list item
    
for number in numbers:
    #do whatever you want here
Source Link
Totem
  • 7.4k
  • 5
  • 43
  • 67

with open("numberlist.txt") as f: # this auto closes the file after reading. It's good practice
    numbers = f.readlines() # numbers is a list of all the numbers(a list of lines in the file)
    
for number in numbers:
    #do whatever you want here