Python File writelines() 方法
python file writelines() 方法
writelines() 方法用于向文件中寫入一序列的字符串。
這一序列字符串可以是由迭代對象產(chǎn)生的,如一個字符串列表。
換行需要制定換行符 \n。
語法
writelines() 方法語法如下:
fileobject.writelines( [ str ])
參數(shù)
-
str -- 要寫入文件的字符串序列。
返回值
該方法沒有返回值。
實(shí)例
以下實(shí)例演示了 writelines() 方法的使用:
#!/usr/bin/python # -*- coding: utf-8 -*- # 打開文件 fo = open("test.txt", "w") print "文件名為: ", fo.name seq = ["<a href="http://www.slktour.com" target="_blank">碩編程</a> 1\n", "<a href="http://www.slktour.com" target="_blank">碩編程</a> 2"] fo.writelines( seq ) # 關(guān)閉文件 fo.close()
以上實(shí)例輸出結(jié)果為:
文件名為: test.txt
查看文件內(nèi)容:
$ cat test.txt <a href="http://www.slktour.com" target="_blank">碩編程</a> 1 <a href="http://www.slktour.com" target="_blank">碩編程</a> 2
相關(guān)文章
- Python 條件語句
- Python for 循環(huán)語句
- Python 列表 List
- Python OS 文件/目錄方法
- Python GUI編程 Tkinter
- Python 數(shù)組
- Python 矩陣
- Python 算法分析
- Python 回溯
- Python 攤銷分析
- Python3 簡介
- Python3 運(yùn)算符
- Python3 元組(Tuple)
- Python3 if else 語句
- Python3 函數(shù)
- Python3 輸入和輸出
- Python3 面向?qū)ο?/a>
- Python3 正則表達(dá)式
- Python3 XML 解析
- Python3 MySQL 數(shù)據(jù)庫連接 - PyMySQL 驅(qū)動