Hi I am new at python, so say I have a text folder with many number records in a single column how would I go about extracting only numbers that begin with zero and is either 10 or 7 digits long creating a new txt file for the 7 digit long numbers, the 10 digit long numbers and finally keeping the rest of the numbers in the original file?

Thanks in advance for your help!

What have you tried so far?

Hint, you can read the file in one line at a time using a for loop. Each line will be a string that can be processed using your criteria.

this is what i got help!!!

input_file = open('xxx_extract.txt', 'rb')
for line in input_file:
if len(line.strip()) == 10
and line[0] == '0'
elif len(line.strip()) == 7
and line[0] == '0'

Here is how you can approach it:

create an empty list:

newVals = []

First of all deal with the file. Open the file, read it to a variable (The hint vegaseat gave you - in the file.readlines()) and then close the file, you have the contents already in 'line'

input_file = open('xxx_extract.txt', 'rb')
line = input_file.readlines()

Now you do your searching through the file for the content you are looking for. That can usually be the headache, but in your case it's simple.

for l in line:
    if len(l.strip()) == 10 or len(l.strip()) == 7:
        if l[0] == '0':

You can print to the console first until you are sure you have it working:

for i in newVals:
    print i

And then you can write to a new file with the same for loop as you used to print above. Just open the file with 'w' for right or 'a' for append.

Well, you were on the right track, but you have to make some modifications. Look at this example ...

# let's assume xxx_extract.txt is ...

input_file = open('xxx_extract.txt', 'r')
# create new output files
out_file = open('new_extract.txt', 'w')
out7_file = open('7_extract.txt', 'w')
out10_file = open('10_extract.txt', 'w')

for line in input_file:
    # strip ending '\n'
    line = line.strip()
    if len(line) == 10 and line[0] == '0':
        # add '\n' back in
        out10_file.write(line + '\n')
    elif len(line) == 7 and line[0] == '0':
        out7_file.write(line + '\n')
        #print(line)  # test
        out_file.write(line + '\n')

print("file has been processed, check new files created")

Use a text editor to check the data in the files created.

This article has been dead for over six months. Start a new discussion instead.