python newbie

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

python newbie

Jerome Jabson
I'm having problems trying to parse a text file and
separating it's contents into a dictionary. The file
as something like:

name
This is the description of the name.

another_name
Another description of this name.

The dictionary should look like:

dict = {name: This is the description of the name.,
another_name: Another discription of this name}

I tried doing something like:

dict = {}
f = open("text-file", "r)
for line in f:
   items = line
    dict[items[0]] = items[1:]
print dict

but the dictionary comes out all wrong! :-( Can
someone give me some pointers on how to do this.

Help is much appreciated!
Jeff

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: python newbie

Jerome Jabson
oops typo:
f = open("text-file", "r)

should be:
f = open("text-file", "r")

--- Jerome Jabson <[hidden email]> wrote:

> I'm having problems trying to parse a text file and
> separating it's contents into a dictionary. The file
> as something like:
>
> name
> This is the description of the name.
>
> another_name
> Another description of this name.
>
> The dictionary should look like:
>
> dict = {name: This is the description of the name.,
> another_name: Another discription of this name}
>
> I tried doing something like:
>
> dict = {}
> f = open("text-file", "r)
> for line in f:
>    items = line
>     dict[items[0]] = items[1:]
> print dict
>
> but the dictionary comes out all wrong! :-( Can
> someone give me some pointers on how to do this.
>
> Help is much appreciated!
> Jeff
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam
> protection around
> http://mail.yahoo.com 
> _______________________________________________
> Baypiggies mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/baypiggies
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: python newbie

Sean Perry
In reply to this post by Jerome Jabson
Jerome Jabson wrote:
> I'm having problems trying to parse a text file and
> separating it's contents into a dictionary. The file
> as something like:
>

there is a fabulous list hosted by python.org called 'tutor'. It is
meant for those new to programming, Python or both. Check it out.
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: python newbie

Brian Mahoney-2
In reply to this post by Jerome Jabson
Typos like that are why it is usually better to post the
actual code.  For coding problems like this, a
recommended resource is
<http://mail.python.org/mailman/listinfo/python-help>

On 3/6/06, Jerome Jabson <[hidden email]> wrote:
> oops typo:
> f = open("text-file", "r)
>
> should be:
> f = open("text-file", "r")
>
> --- Jerome Jabson <[hidden email]> wrote:
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: python newbie

Anna Ravenscroft
In reply to this post by Jerome Jabson


On 3/6/06, Jerome Jabson <[hidden email]> wrote:
I'm having problems trying to parse a text file and
separating it's contents into a dictionary. The file
as something like:

name
This is the description of the name.

another_name
Another description of this name.

The dictionary should look like:

dict = {name: This is the description of the name.,
another_name: Another discription of this name}

I tried doing something like:

dict = {}

don't ever use builtin names for variable names. It'll bite you.
mydict = {}
 

f = open("text-file", "r)
for line in f:
   items = line
    dict[items[0]] = items[1:]
print dict



but the dictionary comes out all wrong! :-(


Describe "all wrong"? From your code above, I suspect you get the first letter of the name as the key, rather than the whole name. You need to split the line into separate words then rejoin them with ' '.join() for the value.

Can
someone give me some pointers on how to do this.


f = open("namer.txt", "r")

mydict = {}

for line in f:
   l = line.split()
   mydict[l[0]] = ' '.join(l[1:])
  
print mydict
 
 
HTH
Anna


_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: python newbie

Tung Wai Yip
In reply to this post by Jerome Jabson
I'd introduce the zen of Python programming, the "print" statement.

dict = {}
f = open("text-file", "r)
for line in f:
    items = line

    # add this an temp working aid
    print 'line - ', items[0], ',', items[1:]

    dict[items[0]] = items[1:]
print dict


It helps you visualize what "item" really looks like. It is not what you  
think.

In this case you should build a little state machine to handle the sequence

   NAME, VALUE, SEPARATOR, NAME, VALUE, SEPARATOR, ... and so on.

There you also have a chance to decide how flexiable you want to allow the  
separators, EOF condition, etc.

Wai Yip


> I'm having problems trying to parse a text file and
> separating it's contents into a dictionary. The file
> as something like:
>
> name
> This is the description of the name.
>
> another_name
> Another description of this name.
>
> The dictionary should look like:
>
> dict = {name: This is the description of the name.,
> another_name: Another discription of this name}
>
> I tried doing something like:
>
> dict = {}
> f = open("text-file", "r)
> for line in f:
>    items = line
>     dict[items[0]] = items[1:]
> print dict
>
> but the dictionary comes out all wrong! :-( Can
> someone give me some pointers on how to do this.
>
> Help is much appreciated!
> Jeff
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
> _______________________________________________
> Baypiggies mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/baypiggies


_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: python newbie

Marilyn Davis
In reply to this post by Sean Perry
On Mon, 6 Mar 2006, Sean Perry wrote:

> Jerome Jabson wrote:
> > I'm having problems trying to parse a text file and
> > separating it's contents into a dictionary. The file
> > as something like:
> >
>
> there is a fabulous list hosted by python.org called 'tutor'. It is
> meant for those new to programming, Python or both. Check it out.

Yes!  The tutor list is a collection of the most patient, generous and
knowledgeable teachers of Python imaginable.

I you want a short class, there's an economical and efficient one this
weekend: $495 in a nice lab in Cupertino.  

The class is motivated by a series of lab exercises.  It's fun.

http://www.ucsc-extension.edu/ucsc/public/category/courseDetails.do?method=load&courseId=3007541&selectedCategoryId=1000075&selectedProgramAreaId=1000171&selectedProgramStreamId=1528553

Marilyn Davis
http://www.deliberate.com/marilyn


> _______________________________________________
> Baypiggies mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/baypiggies
>

--

_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies