我在一个项目中,试图用python编写一个文件,我试图用一个好的格式编写一个文件。我试了很多,但我不知道怎么了?你知道吗
我会试试:
def generate_file(self, lyrics):
self.path()
print('We are writing file ............')
with open('filename.srt', 'w') as lrc:
for i in range(len(lyrics)):
add = ''
if lyrics[i].isnumeric():
add += '\n'
elif lyrics[i].isalpha():
add += '\n\n'
lrc.write(lyrics[i]+add)
add += ''
lrc.close()
print('We downloaded your file!')
输出:
000:00:00 000-->;00:00:00RentAnAdviser.com100公司:00:22“608-->;00:00:26“607喝我的酒喝我的酒哦哦哦啊200:00:26”803-->;00:00:30“602然后我们将穿越交响乐300:00:30”808-->;00:00:38“807然后我们将穿越天空400:00:43”599-->;00:00:48“498Oh天使从上面送来500:00:48”702-->;00:00:53“801你知道你让我的世界照亮了600:00:54”005-->;00:00:59“004当我受伤时我倒下了700:00:59”218-->;00:01:04“717你来把我扶起来800:01:04”911-->;00:01:09“生命是一杯酒,爱是一杯****900:01:09”812-->;00:01:15“011现在我想我必须走1000:01:15”217-->;00:01:20“316当我受伤枯萎干枯1100:01:20”506-->;00:01:26“005你来下雨1200:01:26”217-->;00:01:28“716喝我的,喝我的1300:01:28”900-
我例外:
0
00:00:00000-->;00:00:00000
由RentAnAdviser.com你知道吗
1
00:00:17842-->;00:00:21341
喝我的“喝我的”
2
00:00:21537-->;00:00:23336
然后我们将穿越天空
3
00:00:23546-->;00:00:24545
喝我的“喝我的”
我该怎么做?
我的项目:
from bs4 import BeautifulSoup
import os, requests, platform
class EpicLyricFinderApp:
def __init__(self):
self.text = '%20'.join(input('Enter song name and also include singer: ').split(' '))
self.url = 'https://www.rentanadviser.com/en/subtitles/subtitles4songs.aspx?src='+self.text
self.user = None
self.app()
def app(self):
req = requests.get(self.url).content
soup = BeautifulSoup(req, 'html.parser')
print('Please wait ...................')
tag = soup.findAll('table')
link = [('https://www.rentanadviser.com/en/subtitles/'+l.get('href'))+'&type=srt' for l in [a.find('a') for a in tag]]
blank_name = [''.join((l.get_text()).split(' ')[17:]) for l in [a.find('a') for a in tag]]
[print('No. {} ==>> {}'.format(name+1,blank_name[name])) for name in range(len(blank_name))]
# Get input form user to choice lyrics
print('='*60)
while True:
try:
self.user = int(input('Which lyrics you wanna download?: '))
except ValueError:
continue
else:
break
# Open .srt link
req1 = requests.get(link[self.user]).content
soup1 = BeautifulSoup(req1, 'html.parser')
lyrics = [c.get_text() for c in soup1.findAll('span', attrs={'id':'ctl00_ContentPlaceHolder1_lblSubtitle'})]
self.generate_file(lyrics)
@staticmethod
def path():
if platform.system()=='Linux':
linux = '/home/rohit/Desktop/lrc'
if os.path.exists(linux):
os.chdir(linux)
else:
os.mkdir(linux)
os.chdir(linux)
else:
windows = 'Cd:/Users/ABC/rohit/Desktop/lrc'
if os.path.exists(windows):
os.chdir(windows)
else:
os.mkdir(windows)
os.chdir(windows)
def generate_file(self, lyrics):
self.path()
print('We are writing file ............')
with open('_'.join(self.text.split('%20'))+'.srt', 'w') as lrc:
for i in range(len(lyrics)):
add = ''
if lyrics[i].isnumeric():
add += '\n'
elif lyrics[i].isalpha():
add += '\n\n'
lrc.write(lyrics[i]+add)
add += ''
lrc.close()
print('We downloaded your file!')
if __name__ == '__main__':
app = EpicLyricFinderApp()
目前没有回答
相关问题 更多 >
编程相关推荐