2014年1月19日 星期日

Python basic I/O

Python stdin
ex.
print 'Enter two numbers ...'
m=int(raw_input('Number 1:'))
n=int(raw_input('Number 2:'))

--------------------------------------------------------------------
Python 讀檔

# coding=UTF-8
filename = raw_input('檔名:')

f = open(filename, 'r')
b_str = f.read()
f.close()

print b_str.decode('utf-8') 
print b_str.decode('utf-8').encode('utf-8')


-------------------------------------------
import sys
file = open(sys.argv[1], 'r')
content = file.read()
print content
file.close()

 import sys
file = open(sys.argv[1], 'w')
file.write('test')
file.close()

-----------------------------------------
逐列:
import sys
file = open(sys.argv[1], 'r')
for line in file.readlines():
    print line
file.close()

簡化:
import sys
for line in open(sys.argv[1], 'r'):

    print line

沒有留言:

張貼留言