# Question on write the data from the robot sensor to csv file

**URL:** <https://get-help.theconstruct.ai/t/question-on-write-the-data-from-the-robot-sensor-to-csv-file/11653>\
**Category:** General Support\
**Tags:** error\
**Created:** [August 4, 2021, 3:31pm UTC](https://get-help.theconstruct.ai/t/question-on-write-the-data-from-the-robot-sensor-to-csv-file/11653 "2021-08-04T15:31:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AmirulJ](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AmirulJ](https://get-help.theconstruct.ai/u/AmirulJ)\
**Post date:** [August 4, 2021, 3:31pm UTC](https://get-help.theconstruct.ai/t/question-on-write-the-data-from-the-robot-sensor-to-csv-file/11653/1 "2021-08-04T15:31:21Z")

</div>

This is my code from the sensor, I can print it in the terminal but I cant write it in the csv file automatically. Can someone help me to solve this problem. Thank you in advance!

```auto
#! /usr/bin/env python

import rospy
import csv
from sensor_msgs.msg import LaserScan

def callback(msg):
    d = msg.ranges [90]
    print d

rospy.init_node("read")
sub = rospy.Subscriber('/scan', LaserScan, callback)
rospy.spin()

f= open("\\testread.csv", "w")
c=csv.writer(f)
for x in range(1000):
    c.writerow(d)

f.close()

```

---

<div class="post-metadata">

**Author:** ![albertoezquerro](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/albertoezquerro/32/7031_2.png) [@albertoezquerro](https://get-help.theconstruct.ai/u/albertoezquerro)\
**Post date:** [August 5, 2021, 7:46am UTC](https://get-help.theconstruct.ai/t/question-on-write-the-data-from-the-robot-sensor-to-csv-file/11653/2 "2021-08-05T07:46:55Z")

</div>

Hello @AmirulJ ,

Are you getting any error messages there? In any case, having a quick look at your code, it looks like the variable “`d`” , which contains the laser data, is local to the `callback` function. If you try to use it outside that function, like you are doing here:

`c.writerow(d)`

it won’t work. To fix this, you should define this variable before the callback function.

---

<div class="post-metadata">

**Author:** ![AmirulJ](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AmirulJ](https://get-help.theconstruct.ai/u/AmirulJ)\
**Post date:** [August 5, 2021, 9:56am UTC](https://get-help.theconstruct.ai/t/question-on-write-the-data-from-the-robot-sensor-to-csv-file/11653/3 "2021-08-05T09:56:40Z")

</div>

Thank you very much @albertoezquerro I’ll try first

---

<div class="post-metadata">

**Author:** ![AmirulJ](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AmirulJ](https://get-help.theconstruct.ai/u/AmirulJ)\
**Post date:** [August 6, 2021, 4:39pm UTC](https://get-help.theconstruct.ai/t/question-on-write-the-data-from-the-robot-sensor-to-csv-file/11653/4 "2021-08-06T16:39:16Z")

</div>

Hello @albertoezquerro I’ve try this coding by using global variables, but it still wont work. I got the squence expected error. Can you show me the right coding? Thank you

```auto
#! /usr/bin/env python

import rospy
import csv
from sensor_msgs.msg import LaserScan

def callback(msg):
    global d
    d = msg.ranges [90]
    print d

rospy.init_node("read")
sub = rospy.Subscriber('/scan', LaserScan, callback)
rospy.spin()

f= open("\\testread.csv", "w")
c=csv.writer(f)

for x in range(5)
    c.writerow(d)

f.close()

```

and this I’ve try to define new function but still error

```auto
#! /usr/bin/env python

import rospy
import csv
from sensor_msgs.msg import LaserScan

def laser_ranges()
    global d
    d = msg.ranges [90]
    print d
def callback(msg):
    laser_ranges()

rospy.init_node("read")
sub = rospy.Subscriber('/scan', LaserScan, callback)
rospy.spin()

f= open("\\testread.csv", "w")
c=csv.writer(f)

for x in range(5)
    c.writerow(laser_ranges())

f.close()

```

---

<div class="post-metadata">

**Author:** ![AmirulJ](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AmirulJ](https://get-help.theconstruct.ai/u/AmirulJ)\
**Post date:** [August 8, 2021, 4:02pm UTC](https://get-help.theconstruct.ai/t/question-on-write-the-data-from-the-robot-sensor-to-csv-file/11653/5 "2021-08-08T16:02:18Z")

</div>

Hello @albertoezquerro I’ve try this coding and when executing it, there are no error but the csv file is still empty. May I know why?

```auto
#! /usr/bin/env python
# lds_to_csv_node.py

import rospy
import csv
from sensor_msgs.msg import LaserScan

# Global vars
csv_writer = None

def scan_callback(msg):
    d = msg.ranges [90]
    print d
    if csv_writer is not None:
        csv_writer.writerow(d)

def main():
    rospy.init_node("lds_to_csv")
    sub = rospy.Subscriber('/scan', LaserScan, scan_callback)
    fh = open("\\testread.csv", "wb")
    csv_writer = csv.writer(fh) # More constant params
    rospy.spin()
    fh.close()
    csv_writer = None

if __name__ == ' __main__':
    main()

```

---

<div class="post-metadata">

**Author:** ![system](https://get-help.theconstruct.ai/uploads/default/original/2X/9/9d3ce8305cc0d0d9b6e07d15767a6f245c526f1b.jpeg) [@system](https://get-help.theconstruct.ai/u/system)\
**Post date:** [August 18, 2021, 4:03pm UTC](https://get-help.theconstruct.ai/t/question-on-write-the-data-from-the-robot-sensor-to-csv-file/11653/6 "2021-08-18T16:03:16Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
