以用户输入的格式提取一行文本中的信息 [英] Extract information in a line of text with a format from user input

查看:102
本文介绍了以用户输入的格式提取一行文本中的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个程序,以接收输入的歌曲文件和一种在文件中写入元标记的格式.以下是通话的一些示例:

I am trying to make a program which takes in input song files and a format to write metatags in file. Here is a few examples of the call:

  • ./parser '%n_-_%t.mp3' 01_-_Respect.mp3给了我track=01; title=Respect
  • ./parser '%b._%n.%t.mp3' The_Queen_of_Soul._01.Respect.mp3给了我album=The_Queen_of_Sould; track=01; title=Respect
  • ./parser '%a-%b._%n.%t.mp3' Aretha_Franklin-The_Queen_of_Soul._01.Respect.mp3给了我artist=Aretha_Franklin; track=01; title=Respect
  • ./parser '%a_-_%b_-_%n_-_%t.mp3' Aretha_Franklin_-_The_Queen_of_Soul_-_01_-_Respect.mp3给了我artist=Aretha_Franklin; track=01; title=Respect
  • ./parser '%n_-_%t.mp3' 01_-_Respect.mp3 gives me track=01; title=Respect
  • ./parser '%b._%n.%t.mp3' The_Queen_of_Soul._01.Respect.mp3 gives me album=The_Queen_of_Sould; track=01; title=Respect
  • ./parser '%a-%b._%n.%t.mp3' Aretha_Franklin-The_Queen_of_Soul._01.Respect.mp3 gives me artist=Aretha_Franklin; track=01; title=Respect
  • ./parser '%a_-_%b_-_%n_-_%t.mp3' Aretha_Franklin_-_The_Queen_of_Soul_-_01_-_Respect.mp3 gives me artist=Aretha_Franklin; track=01; title=Respect

对于文件01_-_Respect.mp3的调用,我想要一个包含01的变量,另一个包含Respect的变量.

For a call on the file 01_-_Respect.mp3, I'd like to have a variable containing 01, and the other Respect.

此处%n%t分别代表歌曲的编号和标题.问题是我不知道如何在bash(或最终在python)中提取此信息.

Here %n and %t represents respectively the number and the title of the songs. The problem is that I don't know how to extract this information in bash (or eventually in python).

我最大的问题是我事先不知道格式!

My biggest problem is that I don't know the format in advance!

注意:除此以外,还有更多信息,例如专辑的%b,艺术家的%a等.

Note: There is more information than this, for example %b for the album, %a for the artist etc.

推荐答案

好,您可以使用字符串方法split_-_分割字符串.

Well, you can use the string method split to split the string by _-_.

要从命令行获取输入,可以使用sys.argv来获取.

and for taking the input from the command line, you can use sys.argv to get that.

这是一个例子:

import sys
number,title = sys.argv[1].split("_-_")

更新:

您当然可以像这样将模式作为第一个参数传递,将文件作为第二个参数传递:

Surely you can pass the pattern as a first argument and the file as the second argument like that:

import sys
pattern = sys.argv[1]
number,title = sys.argv[2].split(pattern)

现在,如果您需要更复杂,更动态的处理,那么Regex是您的获奖卡片!

Now if you need more complex and dynamic processing, then Regex is your winning card!

为了编写一个好的正则表达式,您必须了解自己的数据和问题,否则最终会编写出一个小故障的正则表达式

And in order to write a good regex, you got to understand your data and your problem or you'll end up writing a glitchy regex

这篇关于以用户输入的格式提取一行文本中的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆