-1

I have a large file that I need to use search through using RE's

Here is a sample of the file:

76360247039795},{"number":522141635,"catalog"

I need to print out only the digit stream that appears after {"number":

This file is too large to post all of and there is about 200 times this string appears. Which is why I think I need to use RE's and from my research I think what I need to use is something called a "look around" not sure though.

I have been trying this for ages please help

thanks in advance

6
  • 2
    Is it a JSON file? json.org If so, import json; data = json.loads(open("file").read())
    – hughdbrown
    Mar 9, 2013 at 19:14
  • 1
    you have a 9 digit number in your example...
    – CSᵠ
    Mar 9, 2013 at 19:14
  • How is the start and end of string/file, i.e. is it possible to eval it??
    – pradyunsg
    Mar 9, 2013 at 19:15
  • @ka thanks must have miscounted, changed the question so its correct. Mar 9, 2013 at 19:16
  • 2
    So is this question about the files you first described or these other non-JSON files? Definitely use JSON for JSON files; it will be far easier. If this is really about some other files, show what they look like.
    – hughdbrown
    Mar 9, 2013 at 19:22

4 Answers 4

1
import re

with open('myLargeFile.txt', 'r') as myFile:
    numbersList = re.findall('{"number":(\d{9})', myFile.read(), re.DOTALL)
print numbersList

This will create a list that only contains 9 digit numbers that appear after the string {"number":

If the numbers you are looking for might have more or less then 9 digits, use this reg ex instead:

numbersList = re.findall('{"number":(\d{x,y})', myFile.read(), re.DOTALL)

, and replace x and y to fit your needs. x stands for the lowest number of digits that the numbers are allowed to have, and y for the highest. For example, if you want to find all numbers that have in between 5 and 9 digits, the reg ex would become:

numbersList = re.findall('{"number":(\d{5,9})', myFile.read(), re.DOTALL)
1
  • thanks, works brilliantly. but it appears the length varies. how do I change your answer to show the matches of different sizes? Mar 9, 2013 at 19:27
1
import re
s = '76360247039795},{"number":522141635,"catalog"'
nl = re.findall('"number":(\d{9})', s)
0

Try this, using python re module, define a pattern and find your pattern in a string s.

>>> s='76360247039795},{"number":522141635,"catalog"'
>>> import re
>>> re.findall('number":([0-9]+)', s) #this pattern 
['522141635']
>>> re.findall('number":([0-9]{9})', s) #or this pattern works.
['522141635']

(...) Matches whatever regular expression is inside the parentheses and the contents of a group can be retrieved after a match has been performed

2
  • consider putting "number before the "
    – pradyunsg
    Mar 9, 2013 at 19:18
  • that would work but im sure ":[some numbers] appears anywhere else in the file so it would pick up other digit streams Mar 9, 2013 at 19:19
0

For your JSON data, do this:

import json
with open("filename.json") as f:
    data = json.loads(f.read())

Now assuming your file is a list of dictionaries, like this:

[
    {"number": 522141635, "catalog": "..."},
    ....
    {"number": 999999999, "catalog": "..."}
]

print out your numbers as:

for dictionary in data:
    print dictionary["number"]

Not the answer you're looking for? Browse other questions tagged or ask your own question.