Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90959

Re: Best approach to create humongous amount of files

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '"""': 0.07; 'fname': 0.09; 'subject:create': 0.09; 'subject:files': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'creates': 0.14; 'random': 0.14; 'template': 0.14; '"w")': 0.16; '(say': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'integer.': 0.16; 'lowercase': 0.16; 'received:174.136': 0.16; 'sequential': 0.16; 'template,': 0.16; 'folder': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'words': 0.29; 'code': 0.31; 'bunch': 0.31; 'work:': 0.31; 'file': 0.32; 'charset :us-ascii': 0.36; 'too': 0.37; 'received:10': 0.37; 'skip:o 20': 0.38; 'files': 0.38; 'pm,': 0.38; 'skip:x 10': 0.40; 'most': 0.60; 'simple': 0.61; 'to:addr:gmail.com': 0.65; '20,': 0.68; 'million': 0.74; '2015': 0.84; 'stamp': 0.91; 'subject:Best': 0.91; 'dirty': 0.93
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-MC-Relay Neutral
X-MailChannels-SenderId wwwh|x-authuser|tim@thechases.com
X-MailChannels-Auth-Id wwwh
X-MC-Loop-Signature 1432134410894:378854121
X-MC-Ingress-Time 1432134410894
Date Wed, 20 May 2015 10:07:23 -0500
From Tim Chase <python.list@tim.thechases.com>
To Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Subject Re: Best approach to create humongous amount of files
In-Reply-To <CAPTjJmppiMpVjTBt5CH_6DGSdCWw5aoDU+jY-3wMs5Ai7tPdKw@mail.gmail.com>
References <CAPkZ3MS5SiGH9OCe9RSTmakF681O+qM572y49FuDBmBix=aiFg@mail.gmail.com> <CAPTjJmppiMpVjTBt5CH_6DGSdCWw5aoDU+jY-3wMs5Ai7tPdKw@mail.gmail.com>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AuthUser tim@thechases.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.173.1432135585.17265.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432135585 news.xs4all.nl 2967 [2001:888:2000:d::a6]:41443
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90959

Show key headers only | View raw


On 2015-05-20 22:58, Chris Angelico wrote:
> On Wed, May 20, 2015 at 9:44 PM, Parul Mogra <scoria.799@gmail.com>
> wrote:
> > My objective is to create large amount of data files (say a
> > million *.json files), using a pre-existing template file
> > (*.json). Each file would have a unique name, possibly by
> > incorporating time stamp information. The files have to be
> > generated in a folder specified.
[snip] 
> try a simple sequential integer.
> 
> All you'd need would be a loop that creates a bunch of files... most
> of your code will be figuring out what parts of the template need to
> change. Not too difficult.

If you store your template as a Python string-formatting template,
you can just use string-formatting to do your dirty work:


  import random
  HOW_MANY = 1000000
  template = """{
    "some_string": "%(string)s",
    "some_int": %(int)i
    }
    """

  wordlist = [
    word.rstrip()
    for word in open('/usr/share/dict/words')
    ]
  wordlist[:] = [ # just lowercase all-alpha words
    word
    for word in wordlist
    if word.isalpha() and word.islower()
    ]

  for i in xrange(HOW_MANY):
    fname = "data_%08i.json" % i
    with open(fname, "w") as f:
      f.write(template % {
        "string_value": random.choice(wordlist),
        "int_value": random.randint(0, 1000),
        })


-tkc




Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Best approach to create humongous amount of files Tim Chase <python.list@tim.thechases.com> - 2015-05-20 10:07 -0500

csiph-web