Skip to content Skip to sidebar Skip to footer

Python To Extract Specific Numbers From Text File

I have a text file and inside the text file are binary numbers such as 35F,1,0,1,0,0. I want Python to find a specific combination but first there is a degrees number in front. Wha

Solution 1:

There are a few ways, this seems the easiest:

import csv

combination ='1,0,1,0,0'.split(',')

withopen('pythonbigDATA.txt') as infile:
    forrowin csv.reader(infile):
        if row[1:] == combination:
            print row[0], ','.join(row[1:])

Solution 2:

If all lines look like this 00F 0,0,0,0,0 you can use str.split() and keep the part after the first space.

counts = collections.Counter(l.split()[1] for l in infile)

Edit: You can also use split() if there's no space, and the input looks like this: 00F,0,0,0,0,0

counts = collections.Counter(l.split(',',1)[1] for l in infile)

Post a Comment for "Python To Extract Specific Numbers From Text File"