[Tutor] a quick Q: how to use for loop to read a series of files with .doc end
lina
lina.lastname at gmail.com
Fri Oct 7 09:09:30 CEST 2011
On Fri, Oct 7, 2011 at 9:38 AM, Dave Angel <d at davea.name> wrote:
> On 10/06/2011 12:21 PM, lina wrote:
>
>> <snip>
>>
>>
>>> As for splitting into functions, consider:
>>>
>>> #these two are capitalized because they're intended to be constant
>>> TOKENS = "BE"
>>> LINESTOSKIP = 43
>>> INFILEEXT = ".xpm"
>>> OUTFILEEXT = ".txt"
>>>
>>> def dofiles(topdirectory):
>>> for filename in os.listdr(topdirectory):
>>> processfile(filename)
>>>
>>> def processfile(infilename):
>>> base, ext =os.path.splitext(fileName)
>>> if ext == INFILEEXT:
>>> text = fetchonefiledata(infilename)
>>> numcolumns = len(text[0])
>>> results = {}
>>> for ch in TOKENS:
>>>
>>> results[ch] = [0] * numcolumns
>>> for line in text:
>>> line = line.strip()
>>>
>>> for col, ch in enumerate(line):
>>> if ch in tokens:
>>> results[ch][col] += 1
>>>
>>> I still have trouble understanding the results[ch][col] part.
>>
>> Thanks ahead,
>>
>>
>> First ask yourself what results consists of. It's a dictionary, which is
> a aggregate of pairs of key/value items. In each case the key is a
> character, and the the value is a list of numbers.
>
> So results[ch] is a particular value, in other words a list.
>
> And results[ch] [col] is a particular item of the list, in other words, and
> integer
>
> If we use += 1 on that integer, we increment it by 1
>
> Is that clear? if not, write a separate program to build such as
> structure, and try printing out each level of item
> print results["E"]
> should display a list
> print results["E"][0]
> should display an integer
> print results["E"][1]
> print results["E"][2]
>
Yes. I understand this part now. But how can I print a list consists of the
value of key B + E.
For {'B': [4, 5, 6], 'E': [1, 2, 3]}
I wanna get the summary of B and E in each column, namely [5, 7, 9]
>
>
> Does this help?
>
> --
>
> DaveA
>
>
--
Best Regards,
lina
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111007/06a8fd21/attachment.html>
More information about the Tutor
mailing list