Python輸入輸出

輸入內容

def reverse(text):
    return text[::-1]
def is_palindrome(text):
    return text == reverse(text)

something = input('Enter text: ')
if is_palindrome(something):
    print('Yes ,it is a palindrome')
else:
    print('No,it is not a palindrome')
文件操作

poem = '''\
Programing is fun
When the work is done 
if you wanna make your work also fun:
 use Python!
'''

f = open('poem.txt','w')
f.write(poem)
f.close()   #write file

f = open('poem.txt')
while True:
    line = f.readline()
    if len(line) == 0:
        break
    
    print(line,end=' ')

f.close()
Pickle 模塊庫使用
import pickle

shoplistfile = 'shoplist.data'

shoplist = ['apple','mango','carrot']

f = open(shoplistfile,'wb')
pickle.dump(shoplist,f)
f.close()

del shoplist

f = open(shoplistfile,'rb')
storedlist = pickle.load(f)
print(storedlist)
字符編碼

# encoding=utf-8
import io

f = io.open('abc.txt','wt',encoding='utf-8')
f.write(u'Imagin non-Englisj language here')
f.close()

text = io.open('abc.txt',encoding='utf-8').read()
print(text)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章