我刚从http://www.imdb.com/interfaces下载了soundtracks.list文件,我需要帮助将其转换为使用C#的csv文件。我读到了关于imdbpy但我对python一无所知。
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)
这样的事情应该让你开始。但在将来,首先尝试找到解决方案,然后提出具体问题。会有很多人试图在这里帮助你;)
祝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();
}
}
}
}