使用 python 写入 m3u 文件
writing to a m3u file with python
我做了这个脚本我尝试写我在写入文件时用正则表达式请求的内容'w'
只写我尝试的最后一个条目('w'
,'wb'
, 'w+'
) 全部关闭他们写了我做错的最后一个条目?
#-*- coding: utf-8 -*-
import urllib2,urllib
import re
import os
import sys
value=[]
url='https://www.youtube.com/feeds/videos.xmlchannel_id=UCHXdylbsyDVN4UO2Fv8Cgm&API'
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML,like Gecko) Version/7.0.3 Safari/573.75.14')
response = urllib2.urlopen(req)
link=response.read()
response.close()
match=re.compile('<entry>\n <id>.+?</id>\n <yt:videoId>(.+?)</yt:videoId>\n <yt:channelId>.+?</yt:channelId>\n <title>(.*?)\(HD\)</title>').findall(link)
for videoid,isim in match:
#print videoid,isim
name='#EXTINF:-1 ,'+isim+'\n'
link='plugin://plugin.video.youtube/play/?video_id='+videoid+'\n'
value.append((str(name), str(link)))
for name,link in value:
#print name,link
with open('C:\Users\dir\Desktop\muhtesem_yuzyil_kosem.m3u', 'wb+') as f:
f.write(name)
f.write(link)
f.seek(0) # Important: return to the top of the file before reading, otherwise you'll just read an empty string
data = f.read() # Returns 'somedata\n'
f.flush()
f.close()
你的数据写代码有几个问题:
- 您循环打开文件,找到每个
value
项
- 您打开文件只是为了写入
- 您故意在每次迭代中将内部文件句柄位置更改为文件开头,只是为了读取整个文件和
flush
剩下的内容。这并不危险,只是不必要。但是您可能需要一些时间来复习一下您对文件操作的了解。
- 您使用
with
语句,它会自动关闭文件句柄,但随后仍然调用 close()
只打开文件进行一次写入并没有错,然后遍历您的值列表:
with open('C:\Users\dir\Desktop\muhtesem_yuzyil_kosem.m3u', 'wb') as f:
for name,link in value:
f.write(name)
f.write(link)
或者您可以在每次迭代时打开文件,但要确保打开文件进行读写:
for name,link in value:
with open('C:\Users\dir\Desktop\muhtesem_yuzyil_kosem.m3u', 'r+b') as f:
f.seek(0) # Important: return to the top of the file before reading, otherwise you'll just read an empty string
data = f.read() # Returns 'somedata\n'
# make sure that data is written only after cursor was positioned after current file content
f.write(name)
f.write(link)
我做了这个脚本我尝试写我在写入文件时用正则表达式请求的内容'w'
只写我尝试的最后一个条目('w'
,'wb'
, 'w+'
) 全部关闭他们写了我做错的最后一个条目?
#-*- coding: utf-8 -*-
import urllib2,urllib
import re
import os
import sys
value=[]
url='https://www.youtube.com/feeds/videos.xmlchannel_id=UCHXdylbsyDVN4UO2Fv8Cgm&API'
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML,like Gecko) Version/7.0.3 Safari/573.75.14')
response = urllib2.urlopen(req)
link=response.read()
response.close()
match=re.compile('<entry>\n <id>.+?</id>\n <yt:videoId>(.+?)</yt:videoId>\n <yt:channelId>.+?</yt:channelId>\n <title>(.*?)\(HD\)</title>').findall(link)
for videoid,isim in match:
#print videoid,isim
name='#EXTINF:-1 ,'+isim+'\n'
link='plugin://plugin.video.youtube/play/?video_id='+videoid+'\n'
value.append((str(name), str(link)))
for name,link in value:
#print name,link
with open('C:\Users\dir\Desktop\muhtesem_yuzyil_kosem.m3u', 'wb+') as f:
f.write(name)
f.write(link)
f.seek(0) # Important: return to the top of the file before reading, otherwise you'll just read an empty string
data = f.read() # Returns 'somedata\n'
f.flush()
f.close()
你的数据写代码有几个问题:
- 您循环打开文件,找到每个
value
项 - 您打开文件只是为了写入
- 您故意在每次迭代中将内部文件句柄位置更改为文件开头,只是为了读取整个文件和
flush
剩下的内容。这并不危险,只是不必要。但是您可能需要一些时间来复习一下您对文件操作的了解。 - 您使用
with
语句,它会自动关闭文件句柄,但随后仍然调用close()
只打开文件进行一次写入并没有错,然后遍历您的值列表:
with open('C:\Users\dir\Desktop\muhtesem_yuzyil_kosem.m3u', 'wb') as f:
for name,link in value:
f.write(name)
f.write(link)
或者您可以在每次迭代时打开文件,但要确保打开文件进行读写:
for name,link in value:
with open('C:\Users\dir\Desktop\muhtesem_yuzyil_kosem.m3u', 'r+b') as f:
f.seek(0) # Important: return to the top of the file before reading, otherwise you'll just read an empty string
data = f.read() # Returns 'somedata\n'
# make sure that data is written only after cursor was positioned after current file content
f.write(name)
f.write(link)