python TXT文件操作
#coding:utf-8
import MySQLdb
db = MySQLdb.connect()
db.set_character_set('utf8')
cursor = db.cursor()
#select 语句,将所需数据赋值给url_list
url_list = [i[0] for i in cursor.fetchmany(cursor.execute('select vc_url from db_name WHERE vc_class="电影" limit 0,100'))]
#打开文件,wb:写操作,open函数中,如果没有该文件,那么,将会创建该文件
f = open('E:/txt/a.txt','wb')
for i in url_list:
i = i+',' #循环list时加上一个逗号便于之后split()
f.write(i)
f.close()
f = open('E:/txt/a.txt','rb')
#通过split(',')函数将数据重组为一个LIST列表
a = f.read().split(',')
f.close()
for i in a:
print i
f = open('E:/txt/a.txt','w+')
f.truncate()
f.close()
部分参数设置: