As an exercise in learning OOP, I am trying to convert an existing script into OOP form, without success: my current OOP method creates an object that is not iterable <main.rawText object at 0x0000029D55515BA8> TypeError: 'rawText' object is not iterable
The aim of my exercise is to read the content from a CSV file (a collection of product reviews), into a list of lists that will be cleaned up and analysed. How can I produce a list on which I can do list and text operations?
The first script below is my failed attempt, with the working non-OOP version after that
class rawText(object):
def __init__(self, name_file):
self.name_file = name_file
def read_file(self):
"""Read the file concent"""
with open(name_file, 'r') as in_file:
self = in_file.readlines()
return self
def display_file(self):
print(self)
def main():
x = rawText('HCPsentiment2.csv')
x.display_file()
if __name__ == '__main__':
main()
The above produces something that I cannot run content_cleaner on. Below is my original...
# Step 1A - define the content cleaner
def content_cleaner(feed_list):
temp_list = [str(item) for item in feed_list]
temp_list = [item.lower() for item in temp_list]
temp_list = [item.replace("b\'","").replace("\\x93","").replace("\\x94","").replace("\\x96","")
.replace('.','').replace(',','').replace(';','').replace(':','').replace('(','').replace(')','') .replace("'\'","").replace("\\x92","'").replace('"','').replace('"','').replace('[','').replace(']','')
.replace("\\","'")
for item in temp_list]
return list(filter(None, temp_list))
# Step 1B - draw in raw sample text (here a pre-screened csv file)
with open('HCPsentiment2.csv', 'rb') as file:
content = file.readlines()
# perform transformation
content_clean = content_cleaner(content)
# Step 1C - split and clean the sample
content_cl_sp=[phrase.split() for phrase in content_clean]
content_flat = [item for sublist in content_cl_sp for item in sublist]