[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Convert list to file object without creating an actual file.

Bart Kastermans

1/25/2008 2:58:00 AM

I have written a little program that takes as input a text file,
converts
it to a list with appropriate html coding (making it into a nice
table).
Finally I want to upload this list as a textfile using ftp.

If homeworkhtml contains the list of lines;
e.g. homeworkhtml = ["<table>", "<tr>", "<td>", "test", "</td>" .....

I want to call:
ftp.storlines("STOR " + filename, homeworkhtml)

which gives me the error
Traceback (most recent call last):
File "./testhw.py", line 67, in ?
ftp.storlines("STOR " + filename, homeworkhtml)
File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
python2.3/ftplib.py", line 428, in storlines
AttributeError: 'list' object has no attribute 'readline'

Expected since homeworkhtml is in fact not a file. Is there a way
to convert this list to a file object without first writing it to disc
and
then opening the resulting file?

Best,
Bart
26 Answers

Benjamin

1/25/2008 3:57:00 AM

0

On Jan 24, 8:57 pm, Bart Kastermans <bkast...@gmail.com> wrote:
> I have written a little program that takes as input a text file,
> converts
> it to a list with appropriate html coding (making it into a nice
> table).
> Finally I want to upload this list as a textfile using ftp.
>
> If homeworkhtml contains the list of lines;
> e.g. homeworkhtml = ["<table>", "<tr>", "<td>", "test", "</td>" .....
>
> I want to call:
> ftp.storlines("STOR " + filename, homeworkhtml)
>
> which gives me the error
> Traceback (most recent call last):
> File "./testhw.py", line 67, in ?
> ftp.storlines("STOR " + filename, homeworkhtml)
> File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
> python2.3/ftplib.py", line 428, in storlines
> AttributeError: 'list' object has no attribute 'readline'
Perhaps what you want is StringIO. It lets your pretend a string is a
file so ftplib won't choke. You'll have to convert your list to a
string, though (perhaps with join):
from cStringIO import StringIO
fake_file = StringIO("".join(my_list))
>
> Expected since homeworkhtml is in fact not a file. Is there a way
> to convert this list to a file object without first writing it to disc
> and
> then opening the resulting file?
>
> Best,
> Bart

Steven D'Aprano

1/25/2008 4:14:00 AM

0

On Thu, 24 Jan 2008 18:57:58 -0800, Bart Kastermans wrote:

> I have written a little program that takes as input a text file,
....
> Expected since homeworkhtml is in fact not a file. Is there a way to
> convert this list to a file object without first writing it to disc and
> then opening the resulting file?

The StringIO module is your friend, together with a couple of basic
Python techniques.


>>> alist = ["<table>\n", " <tr>\n",
.... " <td>", "Nobody expects the Spanish Inquisition!",
.... "</td>\n", " </tr>\n", "</table>\n"]
>>> print ''.join(alist) # but strings don't have a readlines method...
<table>
<tr>
<td>Nobody expects the Spanish Inquisition!</td>
</tr>
</table>

>>>
>>> f = StringIO.StringIO()
>>> f.writelines(alist)
>>> f.getvalue()
'<table>\n <tr>\n <td>Nobody expects the Spanish Inquisition!</td>\n
</tr>\n</table>\n'
>>> f.seek(0) # don't forget to reset the file pointer!
>>> print f.read() # also has readlines
<table>
<tr>
<td>Nobody expects the Spanish Inquisition!</td>
</tr>
</table>




--
Steven

Bart Kastermans

1/25/2008 2:03:00 PM

0

The suggestion to use StringIO to make a string have the same
interface
as a file works perfect in my situation. Here is now the working
result
(just in case it is useful to anyone) part of this is still a definite
hack and suggestions for improvement are certainly appreciated.

Want:
have an easily maintainable file containing homework assignments, but
have it nicely formatted on my webpage.

Solution:
have a file with due dates and assignments as follows:

Mon Jan 28
1.1: 1,2,3

(at this point only the indentation is taken into account).

Then use the following script (setting some variables and server,
login, and passwd for the ftp connection):

#!/usr/bin/env python

homeworktxt = "homework.txt"
homeworkhtml = []

import sys
import ftplib
import StringIO

Indent = ' '
StartTable = '<table border="1" width="100%"><tr><td>Due</
td><td>Assignment</td></tr>\n<tr>'
EndTable = '</table>'
StartTitle = Indent + '<tr>\n' + Indent + Indent +'<td valign="top">'
EndTitle = Indent + Indent + '</td>\n' + Indent + Indent + '<td>'
BeforeItems = Indent + Indent + Indent + '<table>'
AfterItems = Indent + Indent + Indent + '</table>\n' + Indent +
Indent + '</td>\n' + Indent + '</tr>'
StartItem = Indent + Indent + Indent + Indent + '<tr><td>'
EndItem = Indent + Indent + Indent + Indent + '</td></tr>'


#if 1 >=len (sys.argv):
# print "Need an filename as an argument"
# print sys.argv[0] + " <filename>"
#else:

hw = open (homeworktxt).readlines()

Last = 0 # 0: at the start, 1: just printed an item, 2: just
printed a title

#print StartTable
homeworkhtml.append(StartTable)

for x in hw:
if ' ' == x[0]:
if 2 == Last:
homeworkhtml.append(BeforeItems)
homeworkhtml.append(StartItem)
homeworkhtml.append(Indent + Indent + Indent + Indent + Indent +
x.strip())
homeworkhtml.append(EndItem)
Last = 1
elif '#' != x[0]:
if 1 == Last:
homeworkhtml.append(AfterItems)
homeworkhtml.append(StartTitle)
homeworkhtml.append(Indent + Indent + Indent + x.strip())
homeworkhtml.append(EndTitle)
Last = 2
# else :
# homeworkhtml.append('COMMENT')
# homeworkhtm

if 1 == Last:
homeworkhtml.append(AfterItems)

homeworkhtml.append(EndTable)

for i in range(0,len(homeworkhtml)):
homeworkhtml[i] = homeworkhtml[i] + '\n'

homeworkhtmlfile = StringIO.StringIO()
homeworkhtmlfile.writelines(homeworkhtml)
homeworkhtmlfile.seek(0)

# open connection to the server
ftp = ftplib.FTP('server', 'login', 'password' )

# go to location of root of website on server
ftp.cwd('httpdocs')

# put the contends of a file
filename = "test.html"

ftp.storlines("STOR " + filename, homeworkhtmlfile)

# quite the connection
ftp.quit()



This will result in an html file that you can include (say with
php include) into a webpage displaying a table. Examples of the
result are currently on my webpage (www.bartk.nl/teach.php ) (if
you read this much later look on the waybackmachine).


Again, thanks for the suggestions,

Best,
Bart

PS: apologies for the mixing of spaces and tabs. I had a computer
crash and have not yet corrected the settings. So this is a
combination of default behavior and my preferred settings.

Sancho Panza

7/27/2014 6:51:00 PM

0

On 7/27/2014 1:12 PM, Baxter wrote:
> Sancho Panza <otterpower@xhotmail.com> wrote in
> news:Gv8Bv.91361$Ji3.63452@fx16.iad:
>
>> On 7/27/2014 12:54 AM, GOP_Decline_and_Fall wrote:
>>
>>> The whole point of the Congressman's futile complaint and 15 minutes
>>> of fame was that he was NOT allowed an "anytime visit " but had to
>>> make a future appointment for a group visit under strict no contact
>>> conditions like anyone else.
>>
>> The days when ranking federal officials like members of Congress
>> cannot visit non-classified and non-penal facilities is upon us. Bye
>> bye to "the most transparent administration in U.S. history" that
>> never was. Just one of the earliest of the big lies.
>
> That congressman is a crimial;
>
> http://tinyurl.c...

Stockman took swift and sure action as soon as the violations were
reported. Which is a hell of a lot more than can be said for any of the
numerous scandals perpetrated by the Obama administration.


Baxter

7/27/2014 7:04:00 PM

0

Sancho Panza <otterpower@xhotmail.com> wrote in news:GSbBv.73863
$C87.63521@fx07.iad:

> On 7/27/2014 1:12 PM, Baxter wrote:
>> Sancho Panza <otterpower@xhotmail.com> wrote in
>> news:Gv8Bv.91361$Ji3.63452@fx16.iad:
>>
>>> On 7/27/2014 12:54 AM, GOP_Decline_and_Fall wrote:
>>>
>>>> The whole point of the Congressman's futile complaint and 15 minutes
>>>> of fame was that he was NOT allowed an "anytime visit " but had to
>>>> make a future appointment for a group visit under strict no contact
>>>> conditions like anyone else.
>>>
>>> The days when ranking federal officials like members of Congress
>>> cannot visit non-classified and non-penal facilities is upon us. Bye
>>> bye to "the most transparent administration in U.S. history" that
>>> never was. Just one of the earliest of the big lies.
>>
>> That congressman is a crimial;
>>
>> http://tinyurl.c...
>
> Stockman took swift and sure action as soon as the violations were
> reported. Which is a hell of a lot more than can be said for any of the
> numerous scandals perpetrated by the Obama administration.
>
----------
Noting that paperwork about the alleged resignations was not filed with
House officials until 10 months later, the OCE noted that it "does not
find Representive Stockman's assertion that Mr. Posey and Mr. Dodd were
terminated and rehired in February 2013 to be credible." The OCE board, a
bipartisan group that includes former members of Congress, cited
Stockman's "lack of cooperation" in recommending that he and other staff
members be subpoenaed.

http://tinyurl.c...
---------

Or did you mean this:
--------------
The Texas conservative?s underdog Senate campaign on Monday published a
defiant statement, claiming that Stockman has ?grounds to file criminal
complaints? against any person or media organization that published his
1977 arrest record and mugshot.

http://tinyurl.c...


Steve Stockman Files Libel Suit Alleging He Was Never Arrested For
Smuggling Drugs In His Underpants

http://tinyurl.c...

--------

Personally, I think this is it:
-----
Rep. Steve Stockman: ?If Babies Had Guns They Wouldn?t Be Aborted?

http://tinyurl.c...


--
-----------------------------------------------------
Free Software - Baxter Codeworks www.baxcode.com
-----------------------------------------------------

Big Muddy

7/27/2014 7:09:00 PM

0

On 7/26/2014 7:13 PM, GOP_Decline_and_Fall wrote:
> You live in a dream world.
YOUR mission is fatally compromised Sheila, ruined if you will.

Sheila, I think it's time to chronicle what your game plan is here in
the Uselessnet:

1.) You're a paid DNC hack troll.

2.) Your mission statement is to enter various threads and then ignite
heated debate.

3.) Your tactic is to at first argue topically, but then locate a point
which is unsupportable.

4.) Having identified said point you are charged to see if by supporting
it you can anger conservative posters.

5.) If this succeeds, you are charged to argue ad absurdism until said
conservative posters curse you out for your evident and endless stupidity.

6.) At that point you are charged with demonizing your detractors for
their anger and painting them as representative of Republicans at large.

7.) When the topic becomes too lengthy or the insults dominate, you are
charged with slinking away for a time, even weeks, before returning to
ignite another shit-storm with your support of the unsupportable.

That is who and what you are Sheila, and it is the reason you went to
lengths of absurdity heretofore unseen in trying to maintain that a TV
has no moving parts.

Your mission is now identified for all and thereby corrupted.

What will your DNC handlers task you with next?

Cuz everyone here knows who you are, what you do, why you do it, and who
pays you to do it.

Game over, DNC shill...game fucking over.

Now go play in traffic.


Big Muddy

7/27/2014 7:10:00 PM

0

On 7/26/2014 8:01 PM, GOP_Decline_and_Fall wrote:
> A statement from HHS? Administration for Children and Families (ACF)
YOUR mission is fatally compromised Sheila, ruined if you will.

Sheila, I think it's time to chronicle what your game plan is here in
the Uselessnet:

1.) You're a paid DNC hack troll.

2.) Your mission statement is to enter various threads and then ignite
heated debate.

3.) Your tactic is to at first argue topically, but then locate a point
which is unsupportable.

4.) Having identified said point you are charged to see if by supporting
it you can anger conservative posters.

5.) If this succeeds, you are charged to argue ad absurdism until said
conservative posters curse you out for your evident and endless stupidity.

6.) At that point you are charged with demonizing your detractors for
their anger and painting them as representative of Republicans at large.

7.) When the topic becomes too lengthy or the insults dominate, you are
charged with slinking away for a time, even weeks, before returning to
ignite another shit-storm with your support of the unsupportable.

That is who and what you are Sheila, and it is the reason you went to
lengths of absurdity heretofore unseen in trying to maintain that a TV
has no moving parts.

Your mission is now identified for all and thereby corrupted.

What will your DNC handlers task you with next?

Cuz everyone here knows who you are, what you do, why you do it, and who
pays you to do it.

Game over, DNC shill...game fucking over.

Now go play in traffic.


Big Muddy

7/27/2014 7:10:00 PM

0

On 7/26/2014 8:08 PM, GOP_Decline_and_Fall wrote:
> can't find anything incorrect?
YOUR mission is fatally compromised Sheila, ruined if you will.

Sheila, I think it's time to chronicle what your game plan is here in
the Uselessnet:

1.) You're a paid DNC hack troll.

2.) Your mission statement is to enter various threads and then ignite
heated debate.

3.) Your tactic is to at first argue topically, but then locate a point
which is unsupportable.

4.) Having identified said point you are charged to see if by supporting
it you can anger conservative posters.

5.) If this succeeds, you are charged to argue ad absurdism until said
conservative posters curse you out for your evident and endless stupidity.

6.) At that point you are charged with demonizing your detractors for
their anger and painting them as representative of Republicans at large.

7.) When the topic becomes too lengthy or the insults dominate, you are
charged with slinking away for a time, even weeks, before returning to
ignite another shit-storm with your support of the unsupportable.

That is who and what you are Sheila, and it is the reason you went to
lengths of absurdity heretofore unseen in trying to maintain that a TV
has no moving parts.

Your mission is now identified for all and thereby corrupted.

What will your DNC handlers task you with next?

Cuz everyone here knows who you are, what you do, why you do it, and who
pays you to do it.

Game over, DNC shill...game fucking over.

Now go play in traffic.


Big Muddy

7/27/2014 7:10:00 PM

0

On 7/26/2014 10:54 PM, GOP_Decline_and_Fall wrote:
> As for reporters being able to wander in and out at will...forget it.
YOUR mission is fatally compromised Sheila, ruined if you will.

Sheila, I think it's time to chronicle what your game plan is here in
the Uselessnet:

1.) You're a paid DNC hack troll.

2.) Your mission statement is to enter various threads and then ignite
heated debate.

3.) Your tactic is to at first argue topically, but then locate a point
which is unsupportable.

4.) Having identified said point you are charged to see if by supporting
it you can anger conservative posters.

5.) If this succeeds, you are charged to argue ad absurdism until said
conservative posters curse you out for your evident and endless stupidity.

6.) At that point you are charged with demonizing your detractors for
their anger and painting them as representative of Republicans at large.

7.) When the topic becomes too lengthy or the insults dominate, you are
charged with slinking away for a time, even weeks, before returning to
ignite another shit-storm with your support of the unsupportable.

That is who and what you are Sheila, and it is the reason you went to
lengths of absurdity heretofore unseen in trying to maintain that a TV
has no moving parts.

Your mission is now identified for all and thereby corrupted.

What will your DNC handlers task you with next?

Cuz everyone here knows who you are, what you do, why you do it, and who
pays you to do it.

Game over, DNC shill...game fucking over.

Now go play in traffic.


Big Muddy

7/27/2014 7:11:00 PM

0

On 7/27/2014 10:21 AM, GOP_Decline_and_Fall wrote:
> The idea is absurd.
YOUR mission is fatally compromised Sheila, ruined if you will.

Sheila, I think it's time to chronicle what your game plan is here in
the Uselessnet:

1.) You're a paid DNC hack troll.

2.) Your mission statement is to enter various threads and then ignite
heated debate.

3.) Your tactic is to at first argue topically, but then locate a point
which is unsupportable.

4.) Having identified said point you are charged to see if by supporting
it you can anger conservative posters.

5.) If this succeeds, you are charged to argue ad absurdism until said
conservative posters curse you out for your evident and endless stupidity.

6.) At that point you are charged with demonizing your detractors for
their anger and painting them as representative of Republicans at large.

7.) When the topic becomes too lengthy or the insults dominate, you are
charged with slinking away for a time, even weeks, before returning to
ignite another shit-storm with your support of the unsupportable.

That is who and what you are Sheila, and it is the reason you went to
lengths of absurdity heretofore unseen in trying to maintain that a TV
has no moving parts.

Your mission is now identified for all and thereby corrupted.

What will your DNC handlers task you with next?

Cuz everyone here knows who you are, what you do, why you do it, and who
pays you to do it.

Game over, DNC shill...game fucking over.

Now go play in traffic.