'TypeError: 'TFRecordWriter' object is not subscriptable

I have generated different Tfrecords files. I am trying to read them and put them together in a common file with extension tfrecord.

The code I have tried to generate for this is as follows:

import tensorflow as tf
files_dir = '/C:/Users/RAQUEL/Dropbox/code/code/tf_records'
output_file='/C:/Users/RAQUEL/Dropbox/code/code/tf_examples.tfrecord'
content = os.listdir(files_dir)
writer=tf.python_io.TFRecordWriter(output_file)
writer_index=0

for tf_record in content:
    #Read file
    filename_queue = tf.train.string_input_producer(
      [tf_record], shuffle=True)
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    
    #Write output file
    
    writer[writer_index].write(serialized_example)
    writer_index = (writer_index + 1) 

writer.close()

Traceback:

Traceback (most recent call last):
    File "/content/gdrive/MyDrive/colabnotebooks/Bert_COpenMed/Juntar_tf.py", line 26, in <module>
        writer[writer_index].write(serialized_example)
TypeError: 'TFRecordWriter' object is not subscriptable


Solution 1:[1]

the 'TFRecordWriter' object is not subscriptable. So writer[writer_index] does not work.

My guess is that you need to use soemthing like writer.write(serialized_example).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Burschken