0

I am reading a file (m.csv) line by line such that I create a unit vector for every movie genres. I want to append the resulted vector at the end of each line in the file itself before going to the next line. for instance:

If a line has this movie information:

1,Four Rooms (1995),Comedy

The appended vector will be:

18,Four Rooms (1995),Comedy, [0,1,0,0,0,0]

I just have a problem on how to append the vector back as the following:

genres = ['unknown', 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy']


movieDict = {}
with open(path + 'm.csv') as f:
    temp = ''
    for line in f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            l = row[-1].split('|')
            vector = [0] * 6
            for i in range(len(l)):
                # print ("%s is at index=%s! \n" % (l[i], genres.index(l[i])))
                if l[i] in genres:
                    vector[genres.index(l[i])] = 1
                else:
                    vector[genres.index(l[i])] = 0
            reader[row] = vector # <<<<<< I am stuck here!!

Any help is appreciated.

Kristofer
  • 1,457
  • 2
  • 19
  • 27
  • @Jean-FrançoisFabre Thank you so much. I got this error `AttributeError: '_csv.reader' object has no attribute 'append'`. Is that because i am using python 2.7? – Kristofer Apr 03 '17 at 16:08
  • sorry! I meant `row.append(vector)` – Jean-François Fabre Apr 03 '17 at 16:15
  • 1
    You cannot change the file you are reading from. You must write the modified row to a new file. In addition, as the last field contains comma, it must be enclosed in quotation marks : `1,Four Rooms (1995),Comedy, "[0,1,0,0,0,0]"` – Serge Ballesta Apr 03 '17 at 16:19
  • Also the `for line in f:` is useless (even if harmless...) because the reader will consume all the input file – Serge Ballesta Apr 03 '17 at 16:21

1 Answers1

0

As has been commented trying to read from a file and write from a file at the same time won't work.

You can read all the data into memory (which might not be possible for a big file), then truncate the original file and write the new contents, for example see this question whose first answer does this

with open(path, 'r+') as f:
    lines = f.readlines()
    f.seek(0)
    f.truncate()

The alterative is to open a new file for writing, and read a line from one, then write to the other. You can delete the original file and rename the second afterwards.

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62