Project1

标题: TXT歌词文件拆分 [打印本页]

作者: chd114    时间: 2013-12-15 04:47
标题: TXT歌词文件拆分
  1. def lrckill
  2. #$lrc=load_data("Data/Gtlrc.rxdata").to_s
  3. $lrc=File.open ("文件名称.txt").readlines.to_s
  4. $lrctime=[]#时间轴
  5. $lrcword=[]#歌词
  6. $lrcwords=[]#带#分割的歌词
  7. (0...$lrc.split("[").size).each{|i|$lrctime[i]=""}
  8. (0...$lrc.split("]").size).each{|i|$lrcword[i]=""}
  9. (0...$lrc.split("]").size).each{|i|$lrcwords[i]=""}
  10. $lrcsize=$lrc.split("[").size
  11. for i in 1...$lrcsize
  12. $lrc1=$lrc.split("[")[i].to_s
  13. $lrc2=$lrc1.split("]")[0].to_s
  14. $lrctime[i]="["+$lrc2.split("]")[0].to_s+"]"
  15. $lrcword[i]=$lrc1.split("]")[1].to_s
  16. $lrcwords[i]="#"+$lrc1.split("]")[1].to_s
  17. end
  18. file=File.new('lrctime.txt','w')
  19. file.puts($lrctime)
  20. save_data($lrctime,"Data/lrctime.rxdata")

  21. file=File.new('lrcword.txt','w')
  22. file.puts($lrcword)
  23. save_data($lrcword,"Data/lrcword.rxdata")

  24. file=File.new('lrcwords.txt','w')
  25. file.puts($lrcwords)
  26. save_data($lrcwords,"Data/lrcwords.rxdata")
  27. end
复制代码

作者: 无脑之人    时间: 2013-12-15 10:10
我们呼唤正则表达式
作者: fux2    时间: 2013-12-15 16:37
看到这玩意真怀念啊,我以前也写过一个。
RUBY 代码复制
  1. class String
  2.   @@MultiByteToWideChar  = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  3.   @@WideCharToMultiByte  = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
  4.   def s2u
  5.     i = @@MultiByteToWideChar.call(0, 0, self, -1, nil, 0)
  6.     buffer = "\0" * (i*2)
  7.     @@MultiByteToWideChar.call(0, 0, self, -1, buffer, buffer.size / 2)
  8.     i = @@WideCharToMultiByte.call(65001, 0, buffer, -1, nil, 0, nil, nil)
  9.     result = "\0" * i
  10.     @@WideCharToMultiByte.call(65001, 0, buffer, -1, result, result.size, nil, nil)
  11.     result.chop!
  12.     return result
  13.   end
  14. end
  15.  
  16. class Lrc
  17.  
  18.   Terms = ["歌名:","歌手:","专辑:","制作:"]
  19.  
  20.   attr_accessor :title
  21.   attr_accessor :singer
  22.   attr_accessor :album
  23.   attr_accessor :timeline
  24.   attr_accessor :contents
  25.   attr_accessor :author
  26.  
  27.   def initialize(filename)
  28.     self.load(filename)
  29.   end
  30.  
  31.   def load(filename)
  32.     ori_lrc = File.open(filename,"r").read
  33.     @timeline = ori_lrc.scan(/(?<=\[)\d{2}:\d{2}:\d{2}(?=\])/)
  34.     @contents = ori_lrc.scan(/(?<=\d\])[^\[\]]*(?=\n)/).map{|l| l.s2u}
  35.     @title = $1.s2u if ori_lrc[/\bti:(.+)\b/]
  36.     @singer = $1.s2u if ori_lrc[/\bar:(.+)\b/]
  37.     @album = $1.s2u if ori_lrc[/\bal:(.+)\b/]
  38.     @author = $1.s2u if ori_lrc[/\bby:(.+)\b/]
  39.   end
  40.  
  41.   def to_s
  42.     ret = ""
  43.     ret += Terms[0]+@title+"\n" if @title
  44.     ret += Terms[1]+@singer+"\n" if @singer
  45.     ret += Terms[2]+@album+"\n" if @album
  46.     ret += Terms[3]+@author+"\n" if @author
  47.     @contents.each{|l| ret += l+"\n"}
  48.     return ret
  49.   end
  50.  
  51. end
  52.  
  53. a = Lrc.new("1.lrc")
  54. File.open("lrc.txt","w"){|file| file<<a.to_s}
  55. p a.to_s
  56. msgbox a.to_s if defined?(msgbox)
  57. exit

作者: chd114    时间: 2013-12-16 14:56
fux2 发表于 2013-12-15 16:37
看到这玩意真怀念啊,我以前也写过一个。
class String
  @@MultiByteToWideChar  = Win32API.new('kernel3 ...

话说你是想让我抓虫吗···




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1