views:

393

answers:

6

Hi All,

I've got a basic problem in python, and I would be glad for some help :-)

I have two functions. One that convert a text file to a dictionary. And one that splits a sentence into separate words:

(This is the functiondoc.txt)

def autoparts():

    list_of_parts= open('list_of_parts.txt', 'r')
    for line in list_of_parts:
        k, v= line.split()
        list1.append(k)
        list2.append(v)

    dictionary = dict(zip(k, v))

def splittext(text):
    words = text.split()

    print words

Now I want to make a program that uses these two functions.

(this is the program.txt)

from functiondoc import *

# A and B are keys in the dict. The values are 'rear_bumper' 'back_seat'
text = 'A B'    # Input

# Splits the input into separate strings.
input_ = split_line(text)

Here's the part I cant get right. I need to use the autoparts function to output the values (rear_bumper back_seat), but I'm not sure how to call that function so it does that. I don't think it's that hard. But I can't figure it out...

Kind Regards,

Th

+3  A: 

Some quick points:

  • You should not name Python source files ".txt", you should use ".py".
  • Your indents look wrong, but that might just be Stack Overflow.
  • You need to call the autoparts() function to set up the dictionary.
  • The autoparts() function should probably return the dictionary, to make it usable by other code.
  • When opening a text file, you should use the t mode specifier. On some platforms, the lower-level I/O code must know that it is reading text, so you need to tell it.
unwind
you forgot the fact that "code needs actually to do something". it always help anyway
SilentGhost
Ahh.. so that's why my code is all black.. :\
Thanx
+1  A: 

Don't bother creating the lists first, just go straight to the dictionary:

parts_dict={}
list_of_parts = open('list_of_parts.txt', 'r')
for line in list_of_parts:
        k, v = line.split()
        parts_dict[k] = v

Also, are these keys unique? Because if not some of the values will get overwritten.

wodemoneke
Aslong as you declare ' parts_dict={} ' before the for loop.
Nathan Ross Powell
+2  A: 

Hi,

In addition to all of the other hints and tips, I think you're missing something crucial: your functions actually need to return something.

When you create autoparts() or splittext(), the idea is that this will be a function that you can call, and it can (and should) give something back.

Once you figure out the output that you want your function to have, you need to put it in a return statement.

For example, if you wanted to splittext to return the list of words, rather than print them, you would need the line return words. If you want your autoparts to return the dictionary you've built, you would use return dictionary.

To be more precise (and to answer your comment/question below): you don't want to "return a function that makes a dictionary"; you want to return the dictionary while inside the function. So, the last line of your function should be return dictionary (inside the function!) See, for example, the (accepted!) solution from dbr, above.

I think you need to go back to the beginning and read a book or website about python in particular and programming in general, since you are slightly rusty on some of the concepts. One good one (others are available, of course) is http://diveintopython.org/

Andrew Jaffe
How do I return a function that makes a dictionary? I have tried with return dictionary, but I get the error' ***'return' outside function(filename.py, line xx)
Thanx
A: 

There are a lot of problems with what you've written so far, but your question was how to call the auto parts function. Here's how; first, rename your files to functiondocs.py and program.py - they're python so make them python files.

Next, to call the autoparts function, you simply change your main program listing from:

from functiondoc import *

# A and B are keys in the dict. The values are 'rear_bumper' 'back_seat'
text = 'A B'    # Input

# Splits the input into separate strings.
input_ = split_line(text)

to:

from functiondoc import *

# Call the autparts function
autoparts()

In my opinion, it looks like you're asking us to do a CS homework assignment.. but maybe I'm just cynical ;-)

Jon Cage
A: 

Here's about the simplest way you could do this:

def filetodict(filename):
    return dict(line.split() for line in open(filename))

parts = filetodict("list_of_parts.txt")
print parts

Here's the output:

{'a': 'apple', 'c': 'cheese', 'b': 'bacon', 'e': 'egg', 'd': 'donut'}

The file contents:

a apple
b bacon
c cheese
d donut
e egg
Ryan Ginstrom
+1  A: 

As people have pointed out, you need to use the py extension for python source files. Your files would become "functiondoc.py" and "program.py". This will make your import functiondoc work correctly (as long as they are in the same directory)

The biggest problem with the autoparts function is you never returned anything. The other big problem is you used the wrong variable..

for line in list_of_parts:
    k, v = line.split()
    list1.append(k)
    list2.append(v)

# k and v are now the last line split up, *not* the list you've been constructing.
# The following incorrect line:
dictionary = dict(zip(k, v))
# ...should be:
dictionary = dict(zip(list1, list2))
# ..although you shouldn't use zip for this:

You almost never have to use zip, there are times when it can be useful, but for creating a simple dict, it's incorrect.. Instead of doing..

for line in list_of_parts:
    ...
dictionary = dict(zip(k, v))

..simply create an empty dict before the loop, then do mydict[key_variable] = value_variable

For example, how I might have written the function..

def autoparts():
    # open() returns a file object, not the contents of the file,
    # you need to use .read() or .readlines() to get the actual text
    input_file = open('list_of_parts.txt', 'r')
    all_lines = input_file.read_lines() # reads files as a list (one index per line)

    mydict = {} # initialise a empty dictionary

    for line in list_of_parts:
        k, v = line.split()
        mydict[k] = v

    return mydict # you have to explicitly return stuff, or it returns None
dbr