[Tutor] a quick Q: how to use for loop to read a series of files with .doc end

lina lina.lastname at gmail.com
Thu Oct 6 05:46:48 CEST 2011


On Thu, Oct 6, 2011 at 4:33 AM, Prasad, Ramit <ramit.prasad at jpmorgan.com>wrote:

> >>yes, you're iterating over the keys of a dictionary.  Since it only has
> the key "E", that's what you get.  Try printing dir(results) to see what
> methods might return something other than the key.  Make the language work
> for you.
>
> >Sorry I am not smart.  value?
>
> Dictionaries {} are containers for key/value based pairs like { key :
> value, another_key : value(can be same or repeated) }
>
> For example:
> {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
> The keys here are 'B' and 'E'. The values here are [0, 0, 0, 0, 0, 0] (for
> key 'B') and [2, 1, 4, 0, 1, 0] (for key 'E')
>
> You can get the value of a dictionary by doing: value = dictionary[key]
> You can set the value of a dictionary by doing: dictionary[key] = value
>
> You will get an exception if you try to get a value for a key that is not
> in the dictionary.
> >>> dictionary = {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
> >>> dictionary['F']
> KeyError: 'F'
>

I feel I started to understand what's the keys and values, but still
confused in real practice.

>
> You can iterate over dictionaries in a couple ways
> 1-
> for key in dictionary: # also the same as
>                      # for key in dictionary.keys()
>    value = dictionary[ key ]
>

 def writeonefiledata(outname,results):
    outfile = open(outname,"w")
    for key in results:
        return outfile.write(results[key])

$ python3 counter-vertically-v2.py
Traceback (most recent call last):
  File "counter-vertically-v2.py", line 43, in <module>
    dofiles(".")
  File "counter-vertically-v2.py", line 12, in dofiles
    processfile(filename)
  File "counter-vertically-v2.py", line 29, in processfile
    writeonefiledata(base+OUTFILEEXT,results)
  File "counter-vertically-v2.py", line 39, in writeonefiledata
    return outfile.write(results[key])
TypeError: must be str, not list


2-
> for key, value in dictionary.iteritems():
>    < do something here with >
>

def writeonefiledata(outname,results):
    outfile = open(outname,"w")
    for key, value in results.iteritems():
        return outfile.write(key)

$ python3 counter-vertically-v2.py
Traceback (most recent call last):
  File "counter-vertically-v2.py", line 43, in <module>
    dofiles(".")
  File "counter-vertically-v2.py", line 12, in dofiles
    processfile(filename)
  File "counter-vertically-v2.py", line 29, in processfile
    writeonefiledata(base+OUTFILEEXT,results)
  File "counter-vertically-v2.py", line 38, in writeonefiledata
    for key, value in results.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'

I don't know how to fix above problem.

Thanks for your patience.

>
>
>
> Ramit
>
>
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
>
>
>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Best Regards,

lina
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111006/7940c6c0/attachment.html>


More information about the Tutor mailing list