Skip to content Skip to sidebar Skip to footer

Splitting Data In Python?

it does not work. I want to split data as in code in lines attribute. class movie_analyzer: def __init__(self,s): for c in punctuation: import re

Solution 1:

You can use @property decorator to be able to access the result of the method as a property. See this very simple example of how this decorator might be used:

import random

class Randomizer:
    def __init__(self, lower, upper):
        self.lower = lowerself.upper = upper
    
    @property
    def rand_num(self):
        returnrandom.randint(self.lower, self.upper)

Then, you can access it like so:

>>>randomizer = Randomizer(0, 10)>>>randomizer.rand_num
5
>>>randomizer.rand_num
7
>>>randomizer.rand_num
3

Obviously, this is a useless example; however, you can take this logic and apply it to your situation.

Also, one more thing: you are not passing self to lines. You pass movies, which is unneeded because you can just access it using self.movies. However, if you want to access those variables using self you have to set (in your __init__ method):

self.movielist = []
self.movies = moviefile.readlines()

Solution 2:

To call a function you use movie.lines() along with the argument. What you are doing is just accessing the method declaration. Also, make sure you use self as argument in method definitions and save the parameters you want your Object to have. And it is usually a good practice to keep your imports at the head of the file.

import re

classmovie_analyzer:
    def__init__(self,s):
        for c in punctuation:    
            moviefile = open(s, encoding = "latin-1")
            self.movielist = []
            self.movies = moviefile.readlines()

    @propertydeflines(self):
        for movie in self.movies:
                iflen(movie.strip().split("::")) == 4:
                    a = movie.strip().split("::")
                    self.movielist.append(a)

        return self.movielist

movie = movie_analyzer("movies-modified.dat")

movie.lines()

Post a Comment for "Splitting Data In Python?"