0

Below is the code I got. However, I don't know how to get rid of the last comma. I just want the key separated from the value with a space, and then a comma after it to separate it from the next key.

Also, a question on files. If I open a file and read it, and want to read the file again, is there no way to get the cursor to the beginning of the file without closing and opening the file again?

def dict_to_str(d):
    """ (dict) -> str

    Return a string containing each key and value in d. Keys and
    values are separated by a blank space. Each key-value pair is
    separated by a comma. 

    >>> dict_to_str({3: 4, 5: 6})
    '3 4,5 6'
    """ 
    s = ''
    for (k, v) in d.items:
        s = s + str(k) + " " + str(v) + ','
    return s
3
  • Please separate your questions into separate entries. It would be hard to find proper answer, if you have two completely different questions. Also it won't be very useful for others. What is the connection between converting dict to string and changing position in file without reopening? Commented Jul 17, 2013 at 4:48
  • Just thought I'd squeeze another question in there while I'm at it. Sorry, will keep in mind for next time! Commented Jul 17, 2013 at 4:59
  • Well, it is okay if the question is related to the main one (even loosely). But in your case it looks like it was not. Please read faq - it has some nice explanation on what format of questions is best fit for SO. Commented Jul 17, 2013 at 5:04

2 Answers 2

4

Use join instead:

return ', '.join([str(key) + ' ' + str(value) for key, value in d.items()])

Or more verbosely:

items = []

for key, value in d.items():
    items.append('{0} {1}'.format(key, value))

return ', '.join(items)

As for the file, you can seek back to the beginning:

handle.seek(0)
Sign up to request clarification or add additional context in comments.

4 Comments

Lovely line. It's things like your first example that make me love python.
@Blender: Any reason it is return ', '.join([str(key) + ' ' + str(value) for key, value in d.items()]) instead of return ', '.join(str(key) + ' ' + str(value) for key, value in d.items()) (second example does not create list in between)?
@Tadeck: ' '.join() is faster with a list comprehension, as it has a definite size. I don't remember the actual reason, but something about join having to allocate memory for the string.
@Blender: You seem to be right (proof: ideone.com/GsUTrk). Thanks for clarification.
2

Just return all of the string except the last character, like so -

def dict_to_str(d):
    """ (dict) -> str

    Return a string containing each key and value in d. Keys and
    values are separated by a blank space. Each key-value pair is
    separated by a comma. 

    >>> dict_to_str({3: 4, 5: 6})
    '3 4,5 6'
    """ 
    s = ''
    for (k, v) in d.items():
        s += str(k) + " " + str(v) + ','
    return s[:-1] 

Output Test -

>>> dict_to_str({1:2, 3:4})
'1 2,3 4'

P.S - d.items is a function and you cannot iterate over it, you need to call the function and iterate over the returned list.

For your other question, you can just do fileVar.seek(0)

2 Comments

Ah! Forgot I could use slicing. Your solution is great, much appreciated! :)
Glad to be of help. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.