ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

'String' object has no attribute 'splitlines'

asked 2017-04-01 01:50:05 -0500

wjvh gravatar image

updated 2017-04-01 06:50:23 -0500

I'm trying to parse the output of ros_caffe and republish this in a format ready for speaking. Simon Birrell's node filter_caffe does this but I'm getting the error in the headline.

import sys
print sys.path

import rospy
from filter_caffe import filter_caffe_message
from std_msgs.msg import String

Pub = 0

def received_caffe_message(data):
    global Pub
    filtered_message = filter_caffe_message(data)
    if filtered_message:
       rospy.loginfo("No good hit.")

I think it's related to this portion of the code:

    def filter_caffe_message(caffe_message):
    lines = caffe_message.splitlines()
    if (len(lines) == 0):
        return None
    first_line = lines[0]
    tokens = first_line.split(" ")
    if (len(tokens)<2):
        return None
    raw_probability = tokens[1].split("[")   
    if (len(raw_probability)<2):
        return None
    probability = float(raw_probability[1])
    if (probability>PROBABILITY_THRESHOLD):
        object_list = first_line.split(" - ")[1][10:]
        first_object = object_list.split(",")[0]
        return first_object
    return None

This is the exact error I'm getting:

    [ERROR] [WallTime: 1491028835.534480] bad callback: <function received_caffe_message at 0xb672e6b0>
Traceback (most recent call last):
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/", line 720, in _invoke_callback
  File "/home/ubuntu/catkin_ws/src/filter_caffe/nodes/", line 24, in received_caffe_message
    filtered_message = filter_caffe_message(data)
  File "<string>", line 28, in filter_caffe_message
AttributeError: 'String' object has no attribute 'splitlines'

I understand that splitlines() only works with string objects but I thought that caffe_message was a string?



edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-04-01 03:39:14 -0500

ahendrix gravatar image

I don't know anything about this code, but I think I've spotted the bug. The callback is getting std_msgs.msg.String objects (note the capital S), which are actually a ROS std_msgs/String message that wraps a python string, but splitlines is only valid on python's built-in string type (note the lowercase s). You probably need to pass the data member from the incoming message into filter_caffe_message.

edit flag offensive delete link more


Thanks - I think this could be the issue. However the data member is being passed to filter_caffe_message elsewhere in the code (original question revised to show this) so perhaps it's a problem with how I'm importing or defining the variable or calling the function?

wjvh gravatar image wjvh  ( 2017-04-01 06:47:29 -0500 )edit

Solved this - super obvious now. Thanks ahendrix!

 lines = str(caffe_message).splitlines()
wjvh gravatar image wjvh  ( 2017-04-01 23:09:17 -0500 )edit

answered 2021-07-14 01:29:25 -0500

You used ROS std_msgs.msgs.String. This class has an attribute called data with string type. If you want to use this attribute and its python built-in methods you must change your code



edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-04-01 01:50:05 -0500

Seen: 9,752 times

Last updated: Jul 14 '21