0

i have a csv file i wanna split. how can I split the degree, minutes and second coordinates in column(lat) and (long) into multiple columns in such a way that, all degrees(°) under lat will be in a column, all minutes(') under lat will be in another column while all sec(") under lat will be in another column. Same goes for (long).then remove the (°,',")from the result and be left with splited number And i finnally want to save result as new .csv file. How can I achieve this with python.And The code down there could not work. i don't get how i can do that.

lat            long   
5°78'45"      7°45'21.4"
5°12'45"      7°55'54.4"
7°56'56.5"    5°8'56"

import pandas as pd
import csv
import xlrd
name= "COORD.csv"
df=pd.read_csv(name)
print(df)
for row in pd.read_csv(name):
    list= [(row.split(",")[0]) for row in name]

Traceback (most recent call last):
File "pandas\_libs\parsers.pyx", line 1149, in 
pandas._libs.parsers.TextReader._convert_tokens
File "pandas\_libs\parsers.pyx", line 1279, in 
pandas._libs.parsers.TextReader._convert_with_dtype
File "pandas\_libs\parsers.pyx", line 1295, in 
pandas._libs.parsers.TextReader._string_convert
File "pandas\_libs\parsers.pyx", line 1518, in 
pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 1: 
invalid start byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/Temmy/.PyCharmEdu2019.1/config/scratches/crawl.py", line 5, in 
<module>
df=pd.read_csv(name)
File "C:\Users\Temmy\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\pandas\io\parsers.py", line 685, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\Temmy\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\pandas\io\parsers.py", line 463, in _read
data = parser.read(nrows)
File "C:\Users\Temmy\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\pandas\io\parsers.py", line 1154, in read
ret = self._engine.read(nrows)
File "C:\Users\Temmy\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\pandas\io\parsers.py", line 2048, in read
data = self._reader.read(nrows)
File "pandas\_libs\parsers.pyx", line 879, in 
pandas._libs.parsers.TextReader.read
File "pandas\_libs\parsers.pyx", line 894, in 
pandas._libs.parsers.TextReader._read_low_memory
File "pandas\_libs\parsers.pyx", line 971, in 
pandas._libs.parsers.TextReader._read_rows
File "pandas\_libs\parsers.pyx", line 1103, in 
pandas._libs.parsers.TextReader._convert_column_data
File "pandas\_libs\parsers.pyx", line 1156, in 
pandas._libs.parsers.TextReader._convert_tokens
File "pandas\_libs\parsers.pyx", line 1279, in 
pandas._libs.parsers.TextReader._convert_with_dtype
File "pandas\_libs\parsers.pyx", line 1295, in 
pandas._libs.parsers.TextReader._string_convert
File "pandas\_libs\parsers.pyx", line 1518, in 
pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 1: 
invalid start byte
Temitope
  • 27
  • 5
  • kindly post ur expected output as well – sammywemmy Apr 11 '20 at 00:20
  • [see this page](https://cmdlinetips.com/2018/11/how-to-split-a-text-column-in-pandas/), this explains how to split and expand your result, you just need to rename is after and split again cause you have different symbols – Michael Hsi Apr 11 '20 at 00:31
  • This seems to be an encoding problem. I saved your sample data under "name.txt" and then did : `df = pd.read_csv("name.txt", encoding="utf-8", delim_whitespace=True)` and it worked. It depends on the encoding of your "COORD.csv" file. You can also try the parameter `engine=python` in `read_csv` ([see this post](https://stackoverflow.com/questions/18171739/unicodedecodeerror-when-reading-csv-file-in-pandas-with-python)). – Raphaele Adjerad Apr 11 '20 at 06:25

0 Answers0