# How to declare floating numbers, assuming this is the problem

**URL:** <https://discourse.processing.org/t/how-to-declare-floating-numbers-assuming-this-is-the-problem/32690>\
**Category:** Processing.py\
**Created:** [October 8, 2021, 9:34am UTC](https://discourse.processing.org/t/how-to-declare-floating-numbers-assuming-this-is-the-problem/32690 "2021-10-08T09:34:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [October 8, 2021, 9:34am UTC](https://discourse.processing.org/t/how-to-declare-floating-numbers-assuming-this-is-the-problem/32690/1 "2021-10-08T09:34:56Z")

</div>

I have errors with the following PyProcessing code below.

1. The first error I get is  
TypeError: can’t multiply sequence by non-int of type ‘float’  
on the line 'v\_lat=v\_lat\*0.0174533 ’

I think this is due to v\_lat being a string and needs to be converted to a floating number.

1. So I changed ‘v\_lat=row[1]’ to ‘v\_lat=float(row[1])’ and I get this error  
‘ValueError: invalid literal for float: Latitude’

Appreciate any help/tips with how I can convert string to floating number, assuming this is the cause of the problem. Thanks.

PyProcessing code is:

# convert latitude and longitude values to x,y,z co-ordinates

import csv  
c\_csvfile=‘latlon1.csv’  
c\_eradius=6378.147 # earth radius, used with lat and lon to calc x y z coordinates  
c\_radian=0.0174533 # pi/180

def setup():  
size(500,500)  
background(255)

```
statsFileHandle=open(c_csvfile)
statsData=csv.reader(statsFileHandle)
for row in statsData:
    v_time=row[0]
    v_lat=row[1]
    v_lon=row[2]

```

# convert to radians

```
    v_lat=v_lat*0.0174533 
    v_lon=v_lon*0.0174533 

```

#convert to x,y,z  
v\_x=c\_eradius\*cos(v\_lat)_cos(v\_lon)  
v\_y=c\_eradius_cos(v\_lat)_sin(v\_lon)  
v\_z=c\_eradius_sin(v\_lat)

```
    print(str(v_time)+" : "+str(v_lat)+" : "+str(v_lon))

```

latlon1.csv file has these values:

| Time | Latitude | Longitude |
| --- | --- | --- |
| 2021-06-23 21:41:57 | -34.0459032 | 151.1109382 |
| 2021-06-23 21:42:00 | -34.0459232 | 151.1109829 |
| 2021-06-23 21:42:04 | -34.0460907 | 151.1113508 |
| 2021-06-23 21:42:08 | -34.0461909 | 151.1115345 |
| 2021-06-23 21:42:15 | -34.0461581 | 151.1113933 |
| 2021-06-23 21:42:19 | -34.0461696 | 151.1113905 |
| 2021-06-23 21:42:29 | -34.0461639 | 151.1114605 |
| 2021-06-23 21:42:37 | -34.0461209 | 151.1114227 |
| 2021-06-23 21:42:38 | -34.0461209 | 151.1114227 |

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 8, 2021, 1:13pm UTC](https://discourse.processing.org/t/how-to-declare-floating-numbers-assuming-this-is-the-problem/32690/2 "2021-10-08T13:13:08Z")

</div>

> [@proc8888](#):
>
> ‘ValueError: invalid literal for float: Latitude’

Your “latlon1.csv” file has a title header for its 3 column names.

Obviously neither “Latitude” nor “Longitude” can be converted to a **float()**.

I’m not well versed on Python’s builtin library so I had to look up on how to use its csv module.

This is what I’ve come up w/. However I haven’t tested it at all. Use it at your own risk:

```auto
    global coords
    coords = []

    with open('latlon1.csv') as csvFile:
        # Sniff 1st 256 bytes of file for header presence:
        hasHeader = csv.Sniffer().has_header(csvFile.read(256))
        csvFile.seek(0) # go back to begining of file after the sniff read

        csvReader = csv.reader(csvFile)

        if hasHeader:
            header = next(csvReader) # trick to skip CSV header
            print header

        for row in csvReader: # file safe to iterate now
            stamp = row[0]
            lat = radians(float(row[1]))
            lon = radians(float(row[2]))

            print stamp, lat, lon

            coords.append((stamp, lat, lon))

```

Although mysteriously absent in Python Mode’s web reference:

> **[Reference](https://py.processing.org/reference/)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

You should take a look on Processing’s **loadTable()**, which forum members here are more familiar w/:

> **[loadTable() / Reference](https://processing.org/reference/loadTable_.html)**
>
> Reads the contents of a file or URL and creates a Table object with its values. If a file is specified, it must be located in the sketch's "data" folder. The filename parameter can also be a URL to a …

---

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [October 9, 2021, 1:26am UTC](https://discourse.processing.org/t/how-to-declare-floating-numbers-assuming-this-is-the-problem/32690/3 "2021-10-09T01:26:34Z")

</div>

Thanks @GoToLoop  
Yes, thanks for highlighting that the error refers to the heading line.  
I have been caught before with this heading line thing…  
I thought it was looking at the values rows and that it was referring the value as a string and somehow it knows that it was of latitude format, so I tried to convert it to a float.

So after skipping the first row, the sketch works as expected.  
Cheers

---

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [October 9, 2021, 1:32am UTC](https://discourse.processing.org/t/how-to-declare-floating-numbers-assuming-this-is-the-problem/32690/4 "2021-10-09T01:32:40Z")

</div>

@GoToLoop  
so I skipped first row by creating and checking an on/off boolean variable for first row, so as not to process this first row.  
I also used the radians function as you have suggested above instead of calculating it ‘manually’  
Thanks

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 9, 2021, 9:16pm UTC](https://discourse.processing.org/t/how-to-declare-floating-numbers-assuming-this-is-the-problem/32690/5 "2021-10-09T21:16:38Z")

</div>

> [@proc8888](#):
>
> I also used the radians function as you have suggested…

You can also multiply by DEG\_TO\_RAD for the same effect:  
`lat = radians(float(row[1]))` → `lat = float(row[1]) * DEG_TO_RAD`

> [@proc8888](#):
>
> So I skipped first row by creating and checking an on/off boolean variable for first row,

Didn’t **next()** work for you? `next(csvReader)`

Placing a check inside a loop makes it more complex & slow IMO.

Alternatively you can turn the csv.**reader()** into a **list()** or **tuple()** and then slice its 1st element: `[1:]`

```auto
global coords
coords = []

with open('latlon1.csv') as csvFile:
    for row in tuple(csv.reader(csvFile))[1:]: # skips 1st header row
        stamp = row[0]
        lat = float(row[1]) * DEG_TO_RAD
        lon = float(row[2]) * DEG_TO_RAD

        print stamp, lat, lon

        coords.append((stamp, lat, lon))

```

---

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [October 10, 2021, 1:42am UTC](https://discourse.processing.org/t/how-to-declare-floating-numbers-assuming-this-is-the-problem/32690/6 "2021-10-10T01:42:05Z")

</div>

Thank you for the above, suggestions noted and will test them out. Cheers.
