Tensorflow csv 读取器错误:“字符串中的引用必须由另一个引用转义”



我有一个csv文件,我很确定没有",我正在尝试使用以下代码读取该文件:

filename_queue = tf.train.string_input_producer(["../data/train_no_empty_rows.txt"])
# train_no_empty_rows
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)

record_defaults = [tf.constant(['p'], dtype=tf.string),    # Column 0
               tf.constant(['p'], dtype=tf.string),    # Column 1
               tf.constant(['p'], dtype=tf.string)]   # Column 2 

col1, col2, col3 = tf.decode_csv(
value, record_defaults=record_defaults,field_delim=" ")
features = tf.pack([col2, col3])
with tf.Session() as sess:
  # Start populating the filename queue.
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)
  for i in range(1200):
    # Retrieve a single instance:
    example, label = sess.run([features, col1])
  coord.request_stop()
  coord.join(threads)

但是当我运行它时,我收到此错误:

InvalidArgumentError: Quote inside a string has to be escaped by another quote
 [[Node: DecodeCSV_25 = DecodeCSV[OUT_TYPE=[DT_STRING, DT_STRING, DT_STRING], 
field_delim=" ", 
_device="/job:localhost/replica:0/task:0/cpu:0"]
(ReaderRead_25:1, Const_75, Const_76, Const_77)]]

我想我可以调试,但我找不到它在哪里引用 csv 文件中遇到问题的哪个条目。这是一个相当大的csv文件,前100个左右的条目没有这个问题。正如我所说,我找不到任何",并且'似乎在测试中解析得很好。有什么方法可以找到麻烦的条目吗?

谢谢!

查找麻烦条目的一种方法是在tf.decode_csv()之前添加一个tf.Print()运算符:

# ...
# Prints out the contents of `key` and `value` every time the op executes.
value = tf.Print(value, [key, value])
col1, col2, col3 = tf.decode_csv(
    value, record_defaults=record_defaults, field_delim=" ")
# ...

失败前最后记录的条目应指示哪个输入无效。希望当您进行此修改时,根本原因变得明显。

最新更新