librelist archives

« back to archive

Re: [flask] Build Array From Text File

Re: [flask] Build Array From Text File

From:
Stephen Wood
Date:
2014-02-04 @ 17:43
Ivorywolf,

It sounds like you're asking a few different questions. Correct me if I'm
wrong:

   1. How do I load a text file into a list (an array)?
   2. How do I display a specific index in the list?

There are obviously a few ways you can go about this, and these are mostly
python questions. I recommend you store the list in memory rather than
reading it with every request. Use the before_first_request function to
load a file into a list data structure. There are a fe ways you can
accomplish this.

array = []

with open('my_file.txt') as f:
    array = f.read().splitlines()

Now you have an array that can be referenced by index number for any
request.

# print line 6 in the file
print array[5]

This assumes you know exactly what index in the file you're looking for. If
that's not true then you'll need to use a more complex data structure such
as a dict and parse the values of the file into values in the dictionary.

I hope that helps or at least gives you some ideas.

On Tue, Feb 4, 2014 at 8:54 AM, Iv0ryW0lf <iv0ryw0lf@satxhackers.org> wrote:

> What is the correct way to parse a text file through flask?  I'm trying to
> read a line at a time and add it to an array.  Next I want to display only
> the lines I want.  I have tried storing the text file under static and the
> app root.  Thanks!
>
> James
>



-- 
Stephen Wood
www.heystephenwood.com

Re: [flask] Build Array From Text File

From:
Iv0ryW0lf
Date:
2014-02-05 @ 06:10
Stephen,

Here is a snippet of the code.

@app.route("/list")
def display_list():
     fx = open('show.list','r')
     lines = fx.readlines()
     fx.close()
     return lines

I get an error 500.

On 2/4/2014 11:43 AM, Stephen Wood wrote:
> Ivorywolf,
>
> It sounds like you're asking a few different questions. Correct me if 
> I'm wrong:
>
>  1. How do I load a text file into a list (an array)?
>  2. How do I display a specific index in the list?
>
> There are obviously a few ways you can go about this, and these are 
> mostly python questions. I recommend you store the list in memory 
> rather than reading it with every request. Use the 
> before_first_request function to load a file into a list data 
> structure. There are a fe ways you can accomplish this.
>
> array = []
>
> with open('my_file.txt') as f:
>     array = f.read().splitlines()
>
> Now you have an array that can be referenced by index number for any 
> request.
>
> # print line 6 in the file
> print array[5]
>
> This assumes you know exactly what index in the file you're looking 
> for. If that's not true then you'll need to use a more complex data 
> structure such as a dict and parse the values of the file into values 
> in the dictionary.
>
> I hope that helps or at least gives you some ideas.
>
> On Tue, Feb 4, 2014 at 8:54 AM, Iv0ryW0lf <iv0ryw0lf@satxhackers.org 
> <mailto:iv0ryw0lf@satxhackers.org>> wrote:
>
>     What is the correct way to parse a text file through flask? I'm
>     trying to read a line at a time and add it to an array.  Next I
>     want to display only the lines I want.  I have tried storing the
>     text file under static and the app root.  Thanks!
>
>     James
>
>
>
>
> -- 
> Stephen Wood
> www.heystephenwood.com <http://www.heystephenwood.com>

Re: [flask] Build Array From Text File

From:
Kevin Houlihan
Date:
2014-02-05 @ 09:45
Hi James,

I don't think you can return a list directly like that. Try converting it
to JSON first:

import json

@app.route("/list")
def display_list():
    fx = open('show.list','r')
    lines = fx.readlines()
    fx.close()
    return json.dumps(lines)

Regards,
Kevin


On Wed, Feb 5, 2014 at 6:10 AM, Iv0ryW0lf <iv0ryw0lf@satxhackers.org> wrote:

>  Stephen,
>
> Here is a snippet of the code.
>
> @app.route("/list")
> def display_list():
>     fx = open('show.list','r')
>     lines = fx.readlines()
>     fx.close()
>     return lines
>
> I get an error 500.
>
>
> On 2/4/2014 11:43 AM, Stephen Wood wrote:
>
> Ivorywolf,
>
>  It sounds like you're asking a few different questions. Correct me if
> I'm wrong:
>
>    1. How do I load a text file into a list (an array)?
>     2. How do I display a specific index in the list?
>
>  There are obviously a few ways you can go about this, and these are
> mostly python questions. I recommend you store the list in memory rather
> than reading it with every request. Use the before_first_request function
> to load a file into a list data structure. There are a fe ways you can
> accomplish this.
>
>  array = []
>
> with open('my_file.txt') as f:
>     array = f.read().splitlines()
>
>  Now you have an array that can be referenced by index number for any
> request.
>
>  # print line 6 in the file
> print array[5]
>
>  This assumes you know exactly what index in the file you're looking for.
> If that's not true then you'll need to use a more complex data structure
> such as a dict and parse the values of the file into values in the
> dictionary.
>
>  I hope that helps or at least gives you some ideas.
>
>  On Tue, Feb 4, 2014 at 8:54 AM, Iv0ryW0lf <iv0ryw0lf@satxhackers.org>wrote:
>
>> What is the correct way to parse a text file through flask?  I'm trying
>> to read a line at a time and add it to an array.  Next I want to display
>> only the lines I want.  I have tried storing the text file under static and
>> the app root.  Thanks!
>>
>> James
>>
>
>
>
>  --
> Stephen Wood
> www.heystephenwood.com
>
>
>

Re: [flask] Build Array From Text File

From:
Daniel Fairhead (OMNIvision)
Date:
2014-02-06 @ 11:33
Technically you should return a jsonify(...) rather than json.dumps(...).

The reason is that a view that returns plain text (such as json.dumps) 
will be sent with the MIME type of HTML - flask doesn't know any 
different.  If you return jsonify(...), then it actually returns a full 
response with the correct headers, etc.

Also, it's generally better to read files using a with block, rather 
than open & close directly.  This allows python to automatically close 
the file handle if there is an error.

from flask import jsonify

@app.route("/list")
def display_list():
     with open('show.list','r') as fx:
         lines = fx.readlines()
         return jsonify(lines)


- Dan

On 05/02/2014 09:45, Kevin Houlihan wrote:
> Hi James,
>
> I don't think you can return a list directly like that. Try converting
> it to JSON first:
>
> import json
>
> @app.route("/list")
> def display_list():
>      fx = open('show.list','r')
>      lines = fx.readlines()
>      fx.close()
>      return json.dumps(lines)
>
> Regards,
> Kevin
>
>
> On Wed, Feb 5, 2014 at 6:10 AM, Iv0ryW0lf <iv0ryw0lf@satxhackers.org
> <mailto:iv0ryw0lf@satxhackers.org>> wrote:
>
>     Stephen,
>
>     Here is a snippet of the code.
>
>     @app.route("/list")
>     def display_list():
>          fx = open('show.list','r')
>          lines = fx.readlines()
>          fx.close()
>          return lines
>
>     I get an error 500.
>
>
>     On 2/4/2014 11:43 AM, Stephen Wood wrote:
>>     Ivorywolf,
>>
>>     It sounds like you're asking a few different questions. Correct me
>>     if I'm wrong:
>>
>>      1. How do I load a text file into a list (an array)?
>>      2. How do I display a specific index in the list?
>>
>>     There are obviously a few ways you can go about this, and these
>>     are mostly python questions. I recommend you store the list in
>>     memory rather than reading it with every request. Use the
>>     before_first_request function to load a file into a list data
>>     structure. There are a fe ways you can accomplish this.
>>
>>     array = []
>>
>>     with open('my_file.txt') as f:
>>         array = f.read().splitlines()
>>
>>     Now you have an array that can be referenced by index number for
>>     any request.
>>
>>     # print line 6 in the file
>>     print array[5]
>>
>>     This assumes you know exactly what index in the file you're
>>     looking for. If that's not true then you'll need to use a more
>>     complex data structure such as a dict and parse the values of the
>>     file into values in the dictionary.
>>
>>     I hope that helps or at least gives you some ideas.
>>
>>     On Tue, Feb 4, 2014 at 8:54 AM, Iv0ryW0lf
>>     <iv0ryw0lf@satxhackers.org <mailto:iv0ryw0lf@satxhackers.org>> wrote:
>>
>>         What is the correct way to parse a text file through flask?
>>         I'm trying to read a line at a time and add it to an array.
>>         Next I want to display only the lines I want.  I have tried
>>         storing the text file under static and the app root.  Thanks!
>>
>>         James
>>
>>
>>
>>
>>     --
>>     Stephen Wood
>>     www.heystephenwood.com <http://www.heystephenwood.com>
>
>

Re: [flask] Build Array From Text File

From:
Jaap Broekhuizen
Date:
2014-02-06 @ 11:52
Afaik, jsonify on a list doesnt work because of security concerns.

--
Jaap
Op 6 feb. 2014 12:35 schreef "Daniel Fairhead (OMNIvision)" <
daniel.fairhead@om.org>:

> Technically you should return a jsonify(...) rather than json.dumps(...).
>
> The reason is that a view that returns plain text (such as json.dumps)
> will be sent with the MIME type of HTML - flask doesn't know any
> different.  If you return jsonify(...), then it actually returns a full
> response with the correct headers, etc.
>
> Also, it's generally better to read files using a with block, rather
> than open & close directly.  This allows python to automatically close
> the file handle if there is an error.
>
> from flask import jsonify
>
> @app.route("/list")
> def display_list():
>      with open('show.list','r') as fx:
>          lines = fx.readlines()
>          return jsonify(lines)
>
>
> - Dan
>
> On 05/02/2014 09:45, Kevin Houlihan wrote:
> > Hi James,
> >
> > I don't think you can return a list directly like that. Try converting
> > it to JSON first:
> >
> > import json
> >
> > @app.route("/list")
> > def display_list():
> >      fx = open('show.list','r')
> >      lines = fx.readlines()
> >      fx.close()
> >      return json.dumps(lines)
> >
> > Regards,
> > Kevin
> >
> >
> > On Wed, Feb 5, 2014 at 6:10 AM, Iv0ryW0lf <iv0ryw0lf@satxhackers.org
> > <mailto:iv0ryw0lf@satxhackers.org>> wrote:
> >
> >     Stephen,
> >
> >     Here is a snippet of the code.
> >
> >     @app.route("/list")
> >     def display_list():
> >          fx = open('show.list','r')
> >          lines = fx.readlines()
> >          fx.close()
> >          return lines
> >
> >     I get an error 500.
> >
> >
> >     On 2/4/2014 11:43 AM, Stephen Wood wrote:
> >>     Ivorywolf,
> >>
> >>     It sounds like you're asking a few different questions. Correct me
> >>     if I'm wrong:
> >>
> >>      1. How do I load a text file into a list (an array)?
> >>      2. How do I display a specific index in the list?
> >>
> >>     There are obviously a few ways you can go about this, and these
> >>     are mostly python questions. I recommend you store the list in
> >>     memory rather than reading it with every request. Use the
> >>     before_first_request function to load a file into a list data
> >>     structure. There are a fe ways you can accomplish this.
> >>
> >>     array = []
> >>
> >>     with open('my_file.txt') as f:
> >>         array = f.read().splitlines()
> >>
> >>     Now you have an array that can be referenced by index number for
> >>     any request.
> >>
> >>     # print line 6 in the file
> >>     print array[5]
> >>
> >>     This assumes you know exactly what index in the file you're
> >>     looking for. If that's not true then you'll need to use a more
> >>     complex data structure such as a dict and parse the values of the
> >>     file into values in the dictionary.
> >>
> >>     I hope that helps or at least gives you some ideas.
> >>
> >>     On Tue, Feb 4, 2014 at 8:54 AM, Iv0ryW0lf
> >>     <iv0ryw0lf@satxhackers.org <mailto:iv0ryw0lf@satxhackers.org>>
> wrote:
> >>
> >>         What is the correct way to parse a text file through flask?
> >>         I'm trying to read a line at a time and add it to an array.
> >>         Next I want to display only the lines I want.  I have tried
> >>         storing the text file under static and the app root.  Thanks!
> >>
> >>         James
> >>
> >>
> >>
> >>
> >>     --
> >>     Stephen Wood
> >>     www.heystephenwood.com <http://www.heystephenwood.com>
> >
> >
>
>

Re: [flask] Build Array From Text File

From:
Daniel Fairhead (OMNIvision)
Date:
2014-02-06 @ 11:57
Ah, yes. *facepalm* forgot about all that.  In which case 
jsonify({"list": lines}) would be the best way. (Actually, you could 
even do jsonify({"list": fx.readlines() }) - there's no real reason to 
use a separate variable...

On 06/02/2014 11:52, Jaap Broekhuizen wrote:
> Afaik, jsonify on a list doesnt work because of security concerns.
>
> --
> Jaap
>
> Op 6 feb. 2014 12:35 schreef "Daniel Fairhead (OMNIvision)"
> <daniel.fairhead@om.org <mailto:daniel.fairhead@om.org>>:
>
>     Technically you should return a jsonify(...) rather than
>     json.dumps(...).
>
>     The reason is that a view that returns plain text (such as json.dumps)
>     will be sent with the MIME type of HTML - flask doesn't know any
>     different.  If you return jsonify(...), then it actually returns a full
>     response with the correct headers, etc.
>
>     Also, it's generally better to read files using a with block, rather
>     than open & close directly.  This allows python to automatically close
>     the file handle if there is an error.
>
>     from flask import jsonify
>
>     @app.route("/list")
>     def display_list():
>           with open('show.list','r') as fx:
>               lines = fx.readlines()
>               return jsonify(lines)
>
>
>     - Dan
>
>     On 05/02/2014 09:45, Kevin Houlihan wrote:
>      > Hi James,
>      >
>      > I don't think you can return a list directly like that. Try
>     converting
>      > it to JSON first:
>      >
>      > import json
>      >
>      > @app.route("/list")
>      > def display_list():
>      >      fx = open('show.list','r')
>      >      lines = fx.readlines()
>      >      fx.close()
>      >      return json.dumps(lines)
>      >
>      > Regards,
>      > Kevin
>      >
>      >
>      > On Wed, Feb 5, 2014 at 6:10 AM, Iv0ryW0lf
>     <iv0ryw0lf@satxhackers.org <mailto:iv0ryw0lf@satxhackers.org>
>      > <mailto:iv0ryw0lf@satxhackers.org
>     <mailto:iv0ryw0lf@satxhackers.org>>> wrote:
>      >
>      >     Stephen,
>      >
>      >     Here is a snippet of the code.
>      >
>      >     @app.route("/list")
>      >     def display_list():
>      >          fx = open('show.list','r')
>      >          lines = fx.readlines()
>      >          fx.close()
>      >          return lines
>      >
>      >     I get an error 500.
>      >
>      >
>      >     On 2/4/2014 11:43 AM, Stephen Wood wrote:
>      >>     Ivorywolf,
>      >>
>      >>     It sounds like you're asking a few different questions.
>     Correct me
>      >>     if I'm wrong:
>      >>
>      >>      1. How do I load a text file into a list (an array)?
>      >>      2. How do I display a specific index in the list?
>      >>
>      >>     There are obviously a few ways you can go about this, and these
>      >>     are mostly python questions. I recommend you store the list in
>      >>     memory rather than reading it with every request. Use the
>      >>     before_first_request function to load a file into a list data
>      >>     structure. There are a fe ways you can accomplish this.
>      >>
>      >>     array = []
>      >>
>      >>     with open('my_file.txt') as f:
>      >>         array = f.read().splitlines()
>      >>
>      >>     Now you have an array that can be referenced by index number for
>      >>     any request.
>      >>
>      >>     # print line 6 in the file
>      >>     print array[5]
>      >>
>      >>     This assumes you know exactly what index in the file you're
>      >>     looking for. If that's not true then you'll need to use a more
>      >>     complex data structure such as a dict and parse the values
>     of the
>      >>     file into values in the dictionary.
>      >>
>      >>     I hope that helps or at least gives you some ideas.
>      >>
>      >>     On Tue, Feb 4, 2014 at 8:54 AM, Iv0ryW0lf
>      >>     <iv0ryw0lf@satxhackers.org
>     <mailto:iv0ryw0lf@satxhackers.org> <mailto:iv0ryw0lf@satxhackers.org
>     <mailto:iv0ryw0lf@satxhackers.org>>> wrote:
>      >>
>      >>         What is the correct way to parse a text file through flask?
>      >>         I'm trying to read a line at a time and add it to an array.
>      >>         Next I want to display only the lines I want.  I have tried
>      >>         storing the text file under static and the app root.
>       Thanks!
>      >>
>      >>         James
>      >>
>      >>
>      >>
>      >>
>      >>     --
>      >>     Stephen Wood
>      >> www.heystephenwood.com <http://www.heystephenwood.com>
>     <http://www.heystephenwood.com>
>      >
>      >
>

Re: [flask] Build Array From Text File

From:
Kevin Houlihan
Date:
2014-02-06 @ 13:28
Interesting... Is that why returning a bare list doesn't work? I was under
the impression that returning a dictionary directly would result in it
automatically being dumped to json with the correct Content-Type header,
but I'm not sure where I got that idea. If so is jsonify even necessary?
Could you just do return { 'list': fx.readlines() }

Regards,
Kevin


On Thu, Feb 6, 2014 at 11:57 AM, Daniel Fairhead (OMNIvision) <
daniel.fairhead@om.org> wrote:

> Ah, yes. *facepalm* forgot about all that.  In which case
> jsonify({"list": lines}) would be the best way. (Actually, you could
> even do jsonify({"list": fx.readlines() }) - there's no real reason to
> use a separate variable...
>
> On 06/02/2014 11:52, Jaap Broekhuizen wrote:
> > Afaik, jsonify on a list doesnt work because of security concerns.
> >
> > --
> > Jaap
> >
> > Op 6 feb. 2014 12:35 schreef "Daniel Fairhead (OMNIvision)"
> > <daniel.fairhead@om.org <mailto:daniel.fairhead@om.org>>:
> >
> >     Technically you should return a jsonify(...) rather than
> >     json.dumps(...).
> >
> >     The reason is that a view that returns plain text (such as
> json.dumps)
> >     will be sent with the MIME type of HTML - flask doesn't know any
> >     different.  If you return jsonify(...), then it actually returns a
> full
> >     response with the correct headers, etc.
> >
> >     Also, it's generally better to read files using a with block, rather
> >     than open & close directly.  This allows python to automatically
> close
> >     the file handle if there is an error.
> >
> >     from flask import jsonify
> >
> >     @app.route("/list")
> >     def display_list():
> >           with open('show.list','r') as fx:
> >               lines = fx.readlines()
> >               return jsonify(lines)
> >
> >
> >     - Dan
> >
> >     On 05/02/2014 09:45, Kevin Houlihan wrote:
> >      > Hi James,
> >      >
> >      > I don't think you can return a list directly like that. Try
> >     converting
> >      > it to JSON first:
> >      >
> >      > import json
> >      >
> >      > @app.route("/list")
> >      > def display_list():
> >      >      fx = open('show.list','r')
> >      >      lines = fx.readlines()
> >      >      fx.close()
> >      >      return json.dumps(lines)
> >      >
> >      > Regards,
> >      > Kevin
> >      >
> >      >
> >      > On Wed, Feb 5, 2014 at 6:10 AM, Iv0ryW0lf
> >     <iv0ryw0lf@satxhackers.org <mailto:iv0ryw0lf@satxhackers.org>
> >      > <mailto:iv0ryw0lf@satxhackers.org
> >     <mailto:iv0ryw0lf@satxhackers.org>>> wrote:
> >      >
> >      >     Stephen,
> >      >
> >      >     Here is a snippet of the code.
> >      >
> >      >     @app.route("/list")
> >      >     def display_list():
> >      >          fx = open('show.list','r')
> >      >          lines = fx.readlines()
> >      >          fx.close()
> >      >          return lines
> >      >
> >      >     I get an error 500.
> >      >
> >      >
> >      >     On 2/4/2014 11:43 AM, Stephen Wood wrote:
> >      >>     Ivorywolf,
> >      >>
> >      >>     It sounds like you're asking a few different questions.
> >     Correct me
> >      >>     if I'm wrong:
> >      >>
> >      >>      1. How do I load a text file into a list (an array)?
> >      >>      2. How do I display a specific index in the list?
> >      >>
> >      >>     There are obviously a few ways you can go about this, and
> these
> >      >>     are mostly python questions. I recommend you store the list
> in
> >      >>     memory rather than reading it with every request. Use the
> >      >>     before_first_request function to load a file into a list data
> >      >>     structure. There are a fe ways you can accomplish this.
> >      >>
> >      >>     array = []
> >      >>
> >      >>     with open('my_file.txt') as f:
> >      >>         array = f.read().splitlines()
> >      >>
> >      >>     Now you have an array that can be referenced by index number
> for
> >      >>     any request.
> >      >>
> >      >>     # print line 6 in the file
> >      >>     print array[5]
> >      >>
> >      >>     This assumes you know exactly what index in the file you're
> >      >>     looking for. If that's not true then you'll need to use a
> more
> >      >>     complex data structure such as a dict and parse the values
> >     of the
> >      >>     file into values in the dictionary.
> >      >>
> >      >>     I hope that helps or at least gives you some ideas.
> >      >>
> >      >>     On Tue, Feb 4, 2014 at 8:54 AM, Iv0ryW0lf
> >      >>     <iv0ryw0lf@satxhackers.org
> >     <mailto:iv0ryw0lf@satxhackers.org> <mailto:iv0ryw0lf@satxhackers.org
> >     <mailto:iv0ryw0lf@satxhackers.org>>> wrote:
> >      >>
> >      >>         What is the correct way to parse a text file through
> flask?
> >      >>         I'm trying to read a line at a time and add it to an
> array.
> >      >>         Next I want to display only the lines I want.  I have
> tried
> >      >>         storing the text file under static and the app root.
> >       Thanks!
> >      >>
> >      >>         James
> >      >>
> >      >>
> >      >>
> >      >>
> >      >>     --
> >      >>     Stephen Wood
> >      >> www.heystephenwood.com <http://www.heystephenwood.com>
> >     <http://www.heystephenwood.com>
> >      >
> >      >
> >
>
>

Re: [flask] Build Array From Text File

From:
Dorian Hoxha
Date:
2014-02-08 @ 11:41
Bare list/dict doesn't work cause it's not a string i think.


On Thu, Feb 6, 2014 at 2:28 PM, Kevin Houlihan <kevin@crimsoncookie.com>wrote:

> Interesting... Is that why returning a bare list doesn't work? I was under
> the impression that returning a dictionary directly would result in it
> automatically being dumped to json with the correct Content-Type header,
> but I'm not sure where I got that idea. If so is jsonify even necessary?
> Could you just do return { 'list': fx.readlines() }
>
> Regards,
> Kevin
>
>
> On Thu, Feb 6, 2014 at 11:57 AM, Daniel Fairhead (OMNIvision) <
> daniel.fairhead@om.org> wrote:
>
>> Ah, yes. *facepalm* forgot about all that.  In which case
>> jsonify({"list": lines}) would be the best way. (Actually, you could
>> even do jsonify({"list": fx.readlines() }) - there's no real reason to
>> use a separate variable...
>>
>> On 06/02/2014 11:52, Jaap Broekhuizen wrote:
>> > Afaik, jsonify on a list doesnt work because of security concerns.
>> >
>> > --
>> > Jaap
>> >
>> > Op 6 feb. 2014 12:35 schreef "Daniel Fairhead (OMNIvision)"
>> > <daniel.fairhead@om.org <mailto:daniel.fairhead@om.org>>:
>> >
>> >     Technically you should return a jsonify(...) rather than
>> >     json.dumps(...).
>> >
>> >     The reason is that a view that returns plain text (such as
>> json.dumps)
>> >     will be sent with the MIME type of HTML - flask doesn't know any
>> >     different.  If you return jsonify(...), then it actually returns a
>> full
>> >     response with the correct headers, etc.
>> >
>> >     Also, it's generally better to read files using a with block, rather
>> >     than open & close directly.  This allows python to automatically
>> close
>> >     the file handle if there is an error.
>> >
>> >     from flask import jsonify
>> >
>> >     @app.route("/list")
>> >     def display_list():
>> >           with open('show.list','r') as fx:
>> >               lines = fx.readlines()
>> >               return jsonify(lines)
>> >
>> >
>> >     - Dan
>> >
>> >     On 05/02/2014 09:45, Kevin Houlihan wrote:
>> >      > Hi James,
>> >      >
>> >      > I don't think you can return a list directly like that. Try
>> >     converting
>> >      > it to JSON first:
>> >      >
>> >      > import json
>> >      >
>> >      > @app.route("/list")
>> >      > def display_list():
>> >      >      fx = open('show.list','r')
>> >      >      lines = fx.readlines()
>> >      >      fx.close()
>> >      >      return json.dumps(lines)
>> >      >
>> >      > Regards,
>> >      > Kevin
>> >      >
>> >      >
>> >      > On Wed, Feb 5, 2014 at 6:10 AM, Iv0ryW0lf
>> >     <iv0ryw0lf@satxhackers.org <mailto:iv0ryw0lf@satxhackers.org>
>> >      > <mailto:iv0ryw0lf@satxhackers.org
>> >     <mailto:iv0ryw0lf@satxhackers.org>>> wrote:
>> >      >
>> >      >     Stephen,
>> >      >
>> >      >     Here is a snippet of the code.
>> >      >
>> >      >     @app.route("/list")
>> >      >     def display_list():
>> >      >          fx = open('show.list','r')
>> >      >          lines = fx.readlines()
>> >      >          fx.close()
>> >      >          return lines
>> >      >
>> >      >     I get an error 500.
>> >      >
>> >      >
>> >      >     On 2/4/2014 11:43 AM, Stephen Wood wrote:
>> >      >>     Ivorywolf,
>> >      >>
>> >      >>     It sounds like you're asking a few different questions.
>> >     Correct me
>> >      >>     if I'm wrong:
>> >      >>
>> >      >>      1. How do I load a text file into a list (an array)?
>> >      >>      2. How do I display a specific index in the list?
>> >      >>
>> >      >>     There are obviously a few ways you can go about this, and
>> these
>> >      >>     are mostly python questions. I recommend you store the list
>> in
>> >      >>     memory rather than reading it with every request. Use the
>> >      >>     before_first_request function to load a file into a list
>> data
>> >      >>     structure. There are a fe ways you can accomplish this.
>> >      >>
>> >      >>     array = []
>> >      >>
>> >      >>     with open('my_file.txt') as f:
>> >      >>         array = f.read().splitlines()
>> >      >>
>> >      >>     Now you have an array that can be referenced by index
>> number for
>> >      >>     any request.
>> >      >>
>> >      >>     # print line 6 in the file
>> >      >>     print array[5]
>> >      >>
>> >      >>     This assumes you know exactly what index in the file you're
>> >      >>     looking for. If that's not true then you'll need to use a
>> more
>> >      >>     complex data structure such as a dict and parse the values
>> >     of the
>> >      >>     file into values in the dictionary.
>> >      >>
>> >      >>     I hope that helps or at least gives you some ideas.
>> >      >>
>> >      >>     On Tue, Feb 4, 2014 at 8:54 AM, Iv0ryW0lf
>> >      >>     <iv0ryw0lf@satxhackers.org
>> >     <mailto:iv0ryw0lf@satxhackers.org> <mailto:
>> iv0ryw0lf@satxhackers.org
>> >     <mailto:iv0ryw0lf@satxhackers.org>>> wrote:
>> >      >>
>> >      >>         What is the correct way to parse a text file through
>> flask?
>> >      >>         I'm trying to read a line at a time and add it to an
>> array.
>> >      >>         Next I want to display only the lines I want.  I have
>> tried
>> >      >>         storing the text file under static and the app root.
>> >       Thanks!
>> >      >>
>> >      >>         James
>> >      >>
>> >      >>
>> >      >>
>> >      >>
>> >      >>     --
>> >      >>     Stephen Wood
>> >      >> www.heystephenwood.com <http://www.heystephenwood.com>
>> >     <http://www.heystephenwood.com>
>> >      >
>> >      >
>> >
>>
>>
>

Re: [flask] Build Array From Text File

From:
Iv0ryW0lf
Date:
2014-02-05 @ 16:34
That worked brilliantly!  Thanks!

James

On 2/5/2014 3:45 AM, Kevin Houlihan wrote:
> Hi James,
>
> I don't think you can return a list directly like that. Try converting 
> it to JSON first:
>
> import json
>
> @app.route("/list")
> def display_list():
> fx = open('show.list','r')
> lines = fx.readlines()
> fx.close()
> return json.dumps(lines)
>
> Regards,
> Kevin
>
>
> On Wed, Feb 5, 2014 at 6:10 AM, Iv0ryW0lf <iv0ryw0lf@satxhackers.org 
> <mailto:iv0ryw0lf@satxhackers.org>> wrote:
>
>     Stephen,
>
>     Here is a snippet of the code.
>
>     @app.route("/list")
>     def display_list():
>         fx = open('show.list','r')
>         lines = fx.readlines()
>         fx.close()
>         return lines
>
>     I get an error 500.
>
>
>     On 2/4/2014 11:43 AM, Stephen Wood wrote:
>>     Ivorywolf,
>>
>>     It sounds like you're asking a few different questions. Correct
>>     me if I'm wrong:
>>
>>      1. How do I load a text file into a list (an array)?
>>      2. How do I display a specific index in the list?
>>
>>     There are obviously a few ways you can go about this, and these
>>     are mostly python questions. I recommend you store the list in
>>     memory rather than reading it with every request. Use the
>>     before_first_request function to load a file into a list data
>>     structure. There are a fe ways you can accomplish this.
>>
>>     array = []
>>
>>     with open('my_file.txt') as f:
>>         array = f.read().splitlines()
>>
>>     Now you have an array that can be referenced by index number for
>>     any request.
>>
>>     # print line 6 in the file
>>     print array[5]
>>
>>     This assumes you know exactly what index in the file you're
>>     looking for. If that's not true then you'll need to use a more
>>     complex data structure such as a dict and parse the values of the
>>     file into values in the dictionary.
>>
>>     I hope that helps or at least gives you some ideas.
>>
>>     On Tue, Feb 4, 2014 at 8:54 AM, Iv0ryW0lf
>>     <iv0ryw0lf@satxhackers.org <mailto:iv0ryw0lf@satxhackers.org>> wrote:
>>
>>         What is the correct way to parse a text file through flask? 
>>         I'm trying to read a line at a time and add it to an array. 
>>         Next I want to display only the lines I want.  I have tried
>>         storing the text file under static and the app root.  Thanks!
>>
>>         James
>>
>>
>>
>>
>>     -- 
>>     Stephen Wood
>>     www.heystephenwood.com <http://www.heystephenwood.com>
>
>