[Tutor] a quick Q: how to use for loop to read a series of files with .doc end
andreas.perstinger at gmx.net
Sat Oct 8 07:57:37 CEST 2011
On 2011-10-08 05:34, lina wrote:
> Another minor derived questions:
> for a,b in zip(results['E'],results['B']):
> summary.append(a+b) ## now the summary is '[0,1, 3, 5,
> del summary ## here I wanna remove the first zero, which came
> from the initial double quote "EB...E",
> summary.pop() ## here I wanna remove the last three zeros
> print(summary) ### output is [1,3,5,6]
> summary='\n'.join(str(summary).split(',')) ### wish the result in
> one column,
> with open(base+OUTFILEEXT,"w") as f:
> the final result in the output.txt file is:
> Q1: how can I remove the [1 "[" to only keep 1?
That's a job for a list comprehension (read
for a short introduction):
new_summary = '\n'.join([str(element) for element in summary])
> Q2 how can I improve above expressions?
-) As above, you could use a list comprehension for creating your
summary = [(a + b) for a, b in zip(results['E'], results['B'])]
-) Instead of
I suggest to slice your list:
summary = summary[1:-3]
But this assumes that your summary list will always have one leading
zero and 3 trailing zeros.
-) In your last line
with open(base+OUTFILEEXT,"w") as f:
you don't need to convert summary to a string because it is already one.
More information about the Tutor