2015-11-25 43 views
-3

我剛剛從http://www.imdb.com/interfaces下載了soundtracks.list文件,我需要幫助將它轉換爲使用C#的csv文件。我讀了關於imdbpy,但我不知道python的一切。(C#)解析imdb'soundtracks.list'到csv

soundtracks.list的結構是這樣的:

# #1 Cheerleader Camp (2010) (V) 
- "Drop Dat Booty" 
    Performed by Ken Cain and Ben Forman 
    Written and Produced by Ken Cain and Ben Forman 
- "Endless View" 
    Written and Produced by Sterling 
    Performed by Darlings of the Day 

# #2 Chick (2014) 
- "Number 1 Chick and Number 2 chick by D Player" 

# Star Wars (1977) 
- "Star Wars (Main Theme)" (uncredited) 
    Written by John Williams 
    Performed by London Symphony Orchestra 
- "The Hologram/Binary Sunset" (uncredited) 
    Written by John Williams 
    Performed by London Symphony Orchestra 

我需要將其轉換成這種格式:

movie,song,info 
"#1 Cheerleader Camp (2010) (V)","Drop Dat Booty","Performed by Ken Cain and Ben Forman Written and Produced by Ken Cain and Ben Forman" 
"#1 Cheerleader Camp (2010) (V)","Endless View","Written and Produced by Sterling Performed by Darlings of the Day" 
"#2 Chick (2014)","Number 1 Chick and Number 2 chick by D Player","" 
"Star Wars (1977)","Star Wars (Main Theme) (uncredited)","Written by John Williams Performed by London Symphony Orchestra" 
"Star Wars (1977)","The Hologram/Binary Sunset (uncredited)","Written by John Williams Performed by London Symphony Orchestra" 
+0

您的具體問題是什麼? 「我需要幫助」通常不會提出一個很好的問題 –

+0

我需要關於如何完成的幫助。 – tucosalamanca

+0

你嘗試過做什麼嗎? – Phate01

回答

0

像這樣的東西應該讓你開始。但在未來,首先嚐試找到解決方案,然後提出具體問題。將有很多人試圖在這裏幫助你;)

祝你好運與C#!

using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program3 
    { 
     static void Main(string[] args) 
     { 
      using (var writer = new StreamWriter("output.csv")) 
      using (var reader = new StreamReader("input.txt")) 
      { 
       writer.WriteLine("movie,song,info"); 

       string movie = ""; 
       string song = ""; 
       string info = ""; 

       while (!reader.EndOfStream) 
       { 
        string line = reader.ReadLine(); 

        // movie 
        if (line.StartsWith("# ")) 
        { 
         // forget previous song & info 
         song = ""; 
         info = ""; 
         // remember movie 
         movie = line.Substring(2); 
        } 
        // song 
        else if (line.StartsWith("- ")) 
        { 
         // write song info to csv 
         if (song != "") 
         { 
          writer.WriteLine("\"" + movie + "\", \"" + song + "\", \"" + info + "\""); 
         } 
         // forget previous song info 
         info = ""; 
         // remember song 
         song = line.Substring(2); 
        } 
        // song info 
        else if (line.StartsWith(" ")) 
        { 
         // remember info 
         if (info != "") 
         { 
          info += ""; 
         } 
         info += line; 
        } 
        // end of movie 
        else if (line == "") 
        { 
         // write song info to csv 
         if (song != "") 
         { 
          writer.WriteLine("\"" + movie + "\", \"" + song + "\", \"" + info + "\""); 
         } 
         // forget movie, song & info 
         movie = ""; 
         song = ""; 
         info = ""; 
        } 
       } 

       // write song info to csv 
       if (song != "") 
       { 
        writer.WriteLine("\"" + movie + "\", \"" + song + "\", \"" + info + "\""); 
       } 

       writer.Flush(); 
       writer.Close(); 
      } 
     } 
    } 
}