Alexander Marcussen
Alexander Marcussen

Reputation: 155

Create table from text file

I'm trying to use a big data set from IMDB where i want to move the data from a text file to a sql database

Right now im trying to print the the output in rows and columns, so its easier to put into a database.

f = open("movies.list.txt","r")
movread = f.read()
f.close()


s = raw_input('Search: ')
for ns in movread.split("\n")

    if s in ns:
        print(ns[0:1000])

Output:

  Search: #1 Single

  "#1 Single" (2006)                                2006-????
  "#1 Single" (2006) {Cats and Dogs (#1.4)}         2006
  "#1 Single" (2006) {Finishing a Chapter (#1.5)}   2006
  "#1 Single" (2006) {Is the Grass Greener? (#1.1)} 2006
  "#1 Single" (2006) {Stay (#1.8)}                  2006

The Output i want is like this:

  Search: #1 Single
     Title       Year        Sub title                       Year
  ["#1 Single"] [(2006)]                                   [2006-????]
  ["#1 Single"] [(2006)] [{Cats and Dogs (#1.4)}]          [2006]
  ["#1 Single"] [(2006)] [{Finishing a Chapter (#1.5)}]    [2006]
  ["#1 Single"] [(2006)] [{Is the Grass Greener? (#1.1)}]  [2006]
  ["#1 Single"] [(2006)] [{Stay (#1.8)}]                   [2006]

The reason why im trying to do this is so i can extract only the title in the file and put it into a sql database.

the Titels is sounded by ""

I was thinking that maybe i could split the lines by ex: " and then a space but i have tried different thinks and the wont work

Just ask for further information! :)

Upvotes: 0

Views: 1015

Answers (1)

joemar.ct
joemar.ct

Reputation: 1216

You can fetch the value of each column separately, as follows:

def get_title(x): 
    return x.split('"')[1]

def get_year1(x): 
    return x.split('(')[1].split(')')[0]

def get_subtitle(x):
    try:
        return x.split('{')[1].split('}')[0]
    except IndexError:
        return ''

def get_year2(x): 
    return x.split('}')[-1].strip()

Then, in your loop, get the values for each column and print them in your desired format. For example, for the first one, let's say it's x:

title = get_title(x)
year1 = get_year1(x)
subtitle = get_subtitle(x)
year2 = get_year2(x)

print '[' + title + ']', '[' + year1 + ']', '[' + subtitle + ']', '[' + year2 + ']'

Upvotes: 1

Related Questions