|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
python初学者求助各位前辈
a = [{'http': 'http://183.159.91.225:18118'}, {'http': 'http://101.27.20.34:61234'}, {'http': 'http://115.58.129.100:8118'}]
一个这种的数据我想把它写到一个txt文件中格式如下:
http://183.159.91.225:18118
http://101.27.20.34:61234
http://115.58.129.100:8118
我写的是这样但是不行
file = open("a.txt", "w")
a = [{'http': 'http://183.159.91.225:18118'}, {'http': 'http://101.27.20.34:61234'}, {'http': 'http://115.58.129.100:8118'}]
file.write(a)
# 关闭打开的文件
file.close()
请各位前辈指教,感谢
写入需要字符串格式,a为列表,所以把文件信息整理需要到字符串格式,然后再写入就可以了,中间需要观察可以添加print函数来检验数据格式。
file = open("a.txt", "w")
a = [{'http': 'http://183.159.91.225:18118'}, {'http': 'http://101.27.20.34:61234'},
{'http': 'http://115.58.129.100:8118'}]
# 初始化一个空字符串
urls = ''
# 把每个网址分别加进urls中并换行
for url in a:
urls += url['http'] + '\n'
file.write(urls)
# 关闭打开的文件
file.close()
|
|