获取所需的输出时出现问题(代码后显示) 我尝试了几种不同的方法,但似乎没有任何效果。 以下代码就是我尝试解决此问题时遗忘的内容
song_list = []
def get_songs(): # Just gets all songs ready and loaded for other things
global song_names
global song_amount
global song_list
############### Variables
songs_in_folder = list(os.listdir(input_folder)) # Grabs all files in folder Will add for actual .osz detection later on for this stage.
song_list.append(songs_in_folder) # Adds to song_list which is currently just used for testing else where.
# Checks if files are even in folder or not, does not check extentions, must fix in the future
if songs_in_folder >= 1:
song_amount = 1
else:
print("No Songs were found in the directory!")
time.sleep(2)
exit()
############### Code to clean up file names for users and stuff
for elem in songs_in_folder:
beatmap_name = elem[6:] # Cuts off the first 6 characters which are always 6 different numbers ex) 827212 nameless- Milk Crown On Sonnetica => nameless- Milk Crown On Sonnetica
songs_in_folder = string.strip(beatmap_name,'.osz') # Removes file extention '.osz' leaving just each songs name => nameless- Milk Crown On Sonnetica
######## Add song number and name together
song_list = ("[",song_amount,"]",songs_in_folder) # Fixes lists look without numbers or .osz extention. Easy to read this way.
song_amount = int(song_amount)
song_amount = song_amount + 1
song_list = str(song_list)
song_list = ''.join(song_list)
print(song_list)
get_songs()
问题:
输出结果为:('[', 18, ']', ' nameless - Milk Crown On Sonnetica')
所需的输出/结果应为:[song_number] Song name
请注意,song_number
实际上只是song_amount
更直观的示例[18] nameless- Milk Crown On Sonnetica
如果任何人有任何问题或意见,请对此问题发表评论,我们一见就会尽快与您联系。
答案 0 :(得分:1)
您对字符串格式有疑问。您似乎没有孤立地测试算法组成的步骤,因此您不知道在哪里观察错误。
以下是可能解决您问题的方法:
song_amount = 18
songs_in_folder = "nameless- Milk Crown On Sonnetica"
'[%d] %s' % (song_amount, songs_in_folder)
# '[18] nameless- Milk Crown On Sonnetica'
'[{}] {}'.format(song_amount, songs_in_folder)
# '[18] nameless- Milk Crown On Sonnetica'
# or for Python 3.6+
f'[{song_amount}] {songs_in_folder}'
# '[18] nameless- Milk Crown On Sonnetica'
主要的收获应该是,在从中构造出一个冗长的函数之前,您应该测试每个可能的步骤。