extracting latitude from a string #129
-
Hi: I am trying to extract the latitude from hundreds of files that contain the following listing:
I perform the following read: resulting in output:
where the latitude is -0.36, my intended value. In reading multiple files, the latitude value in the files can vary I applied a split on the '/' as: coord = df.iloc[0,0]
print('Coord ',coord) resulting in output:
lat = coord.split('/')
lati = lat[1][7:12]
print('lati: ', lati) resulting in output: The start of the latitude value always starts in the 7th position |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Apologies for the delayed response. I wouldn't use with open(fn, 'rt') as infile:
# skip "%TITLE%
infile.readline()
# Get the siteid, date string, and lat/lon group by splitting on whitespace
site, date, latlon = infile.readline().split()
# Split the lat/lon group on a comma and convert to floats
lat, lon = map(float, latlon.split(','))
# Skip blank
infile.readline()
# Get the column names from the names that *don't* have commas
columns = infile.readline().split()
# Let pandas parse the rest of the file
df = pd.read_csv(infile, skiprows=2, names=columns) |
Beta Was this translation helpful? Give feedback.
Apologies for the delayed response. I wouldn't use
parse_csv
to handle any of that header, only the later data. This is what I wrote to parse what you provided: