This task is best handled by two nested loops, one on lines and another on the words in each line:
for line in open(thefilepath):
for word in line.split():
dosomethingwith(word)
p72, Python Cookbook 2nd edition, O'REILLY
#################
#!/usr/bin/env python
import os, sys
input_file = sys.stdin
output_file = sys.stdout
input_file = open( sys.argv[ 1 ] )
output_file = open( sys.argv[ 2 ], 'w' )
original = input_file.read()
str1="""#ifdef DEVELP /* Borland C v3.1 version */
unsigned char VoltTable[] = { \n"""
str2="""0 };\n#endif
#ifdef EMBED /* Dunfield assembler version */
void VoltTable(void) asm { \n"""
str3=""" }
#endif\n"""
def numGet( x ):
count = 0
str = original
s = ""
if x == 2:
s += " DB "
else:
s += "\t"
for ch in str:
if ch == '\r':
count += 1
if count % 16 != 0:
s += ','
else:
if x == 1:
s += ',\n\t'
elif x == 2:
s += '\n DB '
elif ch == '\n':
s += ''
else:
s += ch
if x == 2:
return s[ :-1 ]
else:
return s
result = ""
result = str1 + numGet( 1 ) + str2 + numGet( 2 ) + str3
output_file.write(result)
input_file.close()
output_file.close()
2 comments:
>っていうのがあるんですけど、これはどうなんでしょう?
>File (open(thefilepath))の場合、lineにはまるごとstringとしてstoreされるってことかな?
Nop. line will contain a string of a line in this case.
See p165 of Learning Python.
My preference is
for line in open('fileName', 'r').readlines():
doSomething(line)
because it's more explicit than the iterator.
Sorry i am writing from the office and not able to write in Japanese.
ちょっとずつわかってきましたありがとうございます
って仕事中なにしてるんすか^^;
Post a Comment