check GPS string format
I have some GPS coordinate data e.g.
38 41'13.2"N
96 30'23.4"E
How can I check it has a constant format? Sometimes the data is like:
38 41.2342
96 30.1211
I tried using re, but the punction inside the string makes it difficult to pass through.
Ideal format is XX XX'XX.X"(E or N)
I tried
import re
r = re.compile(".* .*'.*..*"N")
if r.match('48 46'55.3"N') is not None:
print 'matches'
taken from here
python string
add a comment |
I have some GPS coordinate data e.g.
38 41'13.2"N
96 30'23.4"E
How can I check it has a constant format? Sometimes the data is like:
38 41.2342
96 30.1211
I tried using re, but the punction inside the string makes it difficult to pass through.
Ideal format is XX XX'XX.X"(E or N)
I tried
import re
r = re.compile(".* .*'.*..*"N")
if r.match('48 46'55.3"N') is not None:
print 'matches'
taken from here
python string
Punctuation should not be a problem. What did you try?
– usr2564301
Nov 19 '18 at 17:02
This will work:[NE]$. If not, more details please.
– usr2564301
Nov 19 '18 at 17:04
1
What exactly is the "correct" format? Do you expect values to be zero-padded? How many decimal places do you accept, and where? Are integers required to have zero'd decimal places? What values are acceptable for the cardinal direction at the end (S,SW,SSW, etc...)?
– Patrick Haugh
Nov 19 '18 at 17:04
add a comment |
I have some GPS coordinate data e.g.
38 41'13.2"N
96 30'23.4"E
How can I check it has a constant format? Sometimes the data is like:
38 41.2342
96 30.1211
I tried using re, but the punction inside the string makes it difficult to pass through.
Ideal format is XX XX'XX.X"(E or N)
I tried
import re
r = re.compile(".* .*'.*..*"N")
if r.match('48 46'55.3"N') is not None:
print 'matches'
taken from here
python string
I have some GPS coordinate data e.g.
38 41'13.2"N
96 30'23.4"E
How can I check it has a constant format? Sometimes the data is like:
38 41.2342
96 30.1211
I tried using re, but the punction inside the string makes it difficult to pass through.
Ideal format is XX XX'XX.X"(E or N)
I tried
import re
r = re.compile(".* .*'.*..*"N")
if r.match('48 46'55.3"N') is not None:
print 'matches'
taken from here
python string
python string
edited Nov 19 '18 at 17:06
WBM
asked Nov 19 '18 at 17:00
WBMWBM
360420
360420
Punctuation should not be a problem. What did you try?
– usr2564301
Nov 19 '18 at 17:02
This will work:[NE]$. If not, more details please.
– usr2564301
Nov 19 '18 at 17:04
1
What exactly is the "correct" format? Do you expect values to be zero-padded? How many decimal places do you accept, and where? Are integers required to have zero'd decimal places? What values are acceptable for the cardinal direction at the end (S,SW,SSW, etc...)?
– Patrick Haugh
Nov 19 '18 at 17:04
add a comment |
Punctuation should not be a problem. What did you try?
– usr2564301
Nov 19 '18 at 17:02
This will work:[NE]$. If not, more details please.
– usr2564301
Nov 19 '18 at 17:04
1
What exactly is the "correct" format? Do you expect values to be zero-padded? How many decimal places do you accept, and where? Are integers required to have zero'd decimal places? What values are acceptable for the cardinal direction at the end (S,SW,SSW, etc...)?
– Patrick Haugh
Nov 19 '18 at 17:04
Punctuation should not be a problem. What did you try?
– usr2564301
Nov 19 '18 at 17:02
Punctuation should not be a problem. What did you try?
– usr2564301
Nov 19 '18 at 17:02
This will work:
[NE]$. If not, more details please.– usr2564301
Nov 19 '18 at 17:04
This will work:
[NE]$. If not, more details please.– usr2564301
Nov 19 '18 at 17:04
1
1
What exactly is the "correct" format? Do you expect values to be zero-padded? How many decimal places do you accept, and where? Are integers required to have zero'd decimal places? What values are acceptable for the cardinal direction at the end (
S, SW, SSW, etc...)?– Patrick Haugh
Nov 19 '18 at 17:04
What exactly is the "correct" format? Do you expect values to be zero-padded? How many decimal places do you accept, and where? Are integers required to have zero'd decimal places? What values are acceptable for the cardinal direction at the end (
S, SW, SSW, etc...)?– Patrick Haugh
Nov 19 '18 at 17:04
add a comment |
2 Answers
2
active
oldest
votes
You haven't escaped your quotes in your example. Notice the " on line 2, and the ' on line 3. This is important so that python knows the quote is part of the string, and not terminating it.
I have also used a slightly more explicit pattern.
import re
r = re.compile("^d{2} d{2}'d{2}.d{1}"[EN]$")
if r.match('48 46'55.3"N') is not None:
print 'matches'
add a comment |
Your punctuation problems are (1) you need to escape the . with a backslash when you want to match the actual decimal point, otherwise it matches any character; and (2) you need to escape the double-quote or otherwise prevent it from terminating your string.
The best way to write this as a readable debuggable regex is to use a Python "raw" string r"like this" which allows backslashes without escaping, and furthermore to triple-quote it, which lets you to use both ' and " inside it without escaping. And since triple-quoted strings allow multi-line expressions, you could even compile in VERBOSE mode, allowing whitespace and comments. Debuggability of your subsequent matching/extraction code is also improved if you use the (?P<...>) named-group syntax in your regex—groups will then be accessible by meaningful names, in the match object's groupdict() output. Taken all together, that gives us:
PATTERNS = [ # a list of alternative acceptable formats
re.compile( r"""
^s* # beginning of string (optional whitespace)
(?P<degrees>d+)[s] # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
(?P<minutes>d+)' # integer number of minutes
(?P<seconds>d+(.d*)?)" # seconds, with optional decimal point and decimal places
(?P<axis>[NE]?) # optional 'N' or 'E' character (remove '?' to make it compulsory)
s*$ # end of string (optional whitespace)
""", re.VERBOSE ),
re.compile( r"""
^s* # beginning of string (optional whitespace)
(?P<degrees>d+)[s] # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
(?P<minutes>d+(.d*)?) # minutes, with optional decimal point and decimal places
(?P<axis>[NE]?) # optional 'N' or 'E' character (remove this line if this is never appropriate in this format)
s*$ # end of string (optional whitespace)
""", re.VERBOSE ),
]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379421%2fcheck-gps-string-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You haven't escaped your quotes in your example. Notice the " on line 2, and the ' on line 3. This is important so that python knows the quote is part of the string, and not terminating it.
I have also used a slightly more explicit pattern.
import re
r = re.compile("^d{2} d{2}'d{2}.d{1}"[EN]$")
if r.match('48 46'55.3"N') is not None:
print 'matches'
add a comment |
You haven't escaped your quotes in your example. Notice the " on line 2, and the ' on line 3. This is important so that python knows the quote is part of the string, and not terminating it.
I have also used a slightly more explicit pattern.
import re
r = re.compile("^d{2} d{2}'d{2}.d{1}"[EN]$")
if r.match('48 46'55.3"N') is not None:
print 'matches'
add a comment |
You haven't escaped your quotes in your example. Notice the " on line 2, and the ' on line 3. This is important so that python knows the quote is part of the string, and not terminating it.
I have also used a slightly more explicit pattern.
import re
r = re.compile("^d{2} d{2}'d{2}.d{1}"[EN]$")
if r.match('48 46'55.3"N') is not None:
print 'matches'
You haven't escaped your quotes in your example. Notice the " on line 2, and the ' on line 3. This is important so that python knows the quote is part of the string, and not terminating it.
I have also used a slightly more explicit pattern.
import re
r = re.compile("^d{2} d{2}'d{2}.d{1}"[EN]$")
if r.match('48 46'55.3"N') is not None:
print 'matches'
answered Nov 19 '18 at 17:11
Jim WrightJim Wright
4,2641526
4,2641526
add a comment |
add a comment |
Your punctuation problems are (1) you need to escape the . with a backslash when you want to match the actual decimal point, otherwise it matches any character; and (2) you need to escape the double-quote or otherwise prevent it from terminating your string.
The best way to write this as a readable debuggable regex is to use a Python "raw" string r"like this" which allows backslashes without escaping, and furthermore to triple-quote it, which lets you to use both ' and " inside it without escaping. And since triple-quoted strings allow multi-line expressions, you could even compile in VERBOSE mode, allowing whitespace and comments. Debuggability of your subsequent matching/extraction code is also improved if you use the (?P<...>) named-group syntax in your regex—groups will then be accessible by meaningful names, in the match object's groupdict() output. Taken all together, that gives us:
PATTERNS = [ # a list of alternative acceptable formats
re.compile( r"""
^s* # beginning of string (optional whitespace)
(?P<degrees>d+)[s] # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
(?P<minutes>d+)' # integer number of minutes
(?P<seconds>d+(.d*)?)" # seconds, with optional decimal point and decimal places
(?P<axis>[NE]?) # optional 'N' or 'E' character (remove '?' to make it compulsory)
s*$ # end of string (optional whitespace)
""", re.VERBOSE ),
re.compile( r"""
^s* # beginning of string (optional whitespace)
(?P<degrees>d+)[s] # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
(?P<minutes>d+(.d*)?) # minutes, with optional decimal point and decimal places
(?P<axis>[NE]?) # optional 'N' or 'E' character (remove this line if this is never appropriate in this format)
s*$ # end of string (optional whitespace)
""", re.VERBOSE ),
]
add a comment |
Your punctuation problems are (1) you need to escape the . with a backslash when you want to match the actual decimal point, otherwise it matches any character; and (2) you need to escape the double-quote or otherwise prevent it from terminating your string.
The best way to write this as a readable debuggable regex is to use a Python "raw" string r"like this" which allows backslashes without escaping, and furthermore to triple-quote it, which lets you to use both ' and " inside it without escaping. And since triple-quoted strings allow multi-line expressions, you could even compile in VERBOSE mode, allowing whitespace and comments. Debuggability of your subsequent matching/extraction code is also improved if you use the (?P<...>) named-group syntax in your regex—groups will then be accessible by meaningful names, in the match object's groupdict() output. Taken all together, that gives us:
PATTERNS = [ # a list of alternative acceptable formats
re.compile( r"""
^s* # beginning of string (optional whitespace)
(?P<degrees>d+)[s] # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
(?P<minutes>d+)' # integer number of minutes
(?P<seconds>d+(.d*)?)" # seconds, with optional decimal point and decimal places
(?P<axis>[NE]?) # optional 'N' or 'E' character (remove '?' to make it compulsory)
s*$ # end of string (optional whitespace)
""", re.VERBOSE ),
re.compile( r"""
^s* # beginning of string (optional whitespace)
(?P<degrees>d+)[s] # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
(?P<minutes>d+(.d*)?) # minutes, with optional decimal point and decimal places
(?P<axis>[NE]?) # optional 'N' or 'E' character (remove this line if this is never appropriate in this format)
s*$ # end of string (optional whitespace)
""", re.VERBOSE ),
]
add a comment |
Your punctuation problems are (1) you need to escape the . with a backslash when you want to match the actual decimal point, otherwise it matches any character; and (2) you need to escape the double-quote or otherwise prevent it from terminating your string.
The best way to write this as a readable debuggable regex is to use a Python "raw" string r"like this" which allows backslashes without escaping, and furthermore to triple-quote it, which lets you to use both ' and " inside it without escaping. And since triple-quoted strings allow multi-line expressions, you could even compile in VERBOSE mode, allowing whitespace and comments. Debuggability of your subsequent matching/extraction code is also improved if you use the (?P<...>) named-group syntax in your regex—groups will then be accessible by meaningful names, in the match object's groupdict() output. Taken all together, that gives us:
PATTERNS = [ # a list of alternative acceptable formats
re.compile( r"""
^s* # beginning of string (optional whitespace)
(?P<degrees>d+)[s] # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
(?P<minutes>d+)' # integer number of minutes
(?P<seconds>d+(.d*)?)" # seconds, with optional decimal point and decimal places
(?P<axis>[NE]?) # optional 'N' or 'E' character (remove '?' to make it compulsory)
s*$ # end of string (optional whitespace)
""", re.VERBOSE ),
re.compile( r"""
^s* # beginning of string (optional whitespace)
(?P<degrees>d+)[s] # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
(?P<minutes>d+(.d*)?) # minutes, with optional decimal point and decimal places
(?P<axis>[NE]?) # optional 'N' or 'E' character (remove this line if this is never appropriate in this format)
s*$ # end of string (optional whitespace)
""", re.VERBOSE ),
]
Your punctuation problems are (1) you need to escape the . with a backslash when you want to match the actual decimal point, otherwise it matches any character; and (2) you need to escape the double-quote or otherwise prevent it from terminating your string.
The best way to write this as a readable debuggable regex is to use a Python "raw" string r"like this" which allows backslashes without escaping, and furthermore to triple-quote it, which lets you to use both ' and " inside it without escaping. And since triple-quoted strings allow multi-line expressions, you could even compile in VERBOSE mode, allowing whitespace and comments. Debuggability of your subsequent matching/extraction code is also improved if you use the (?P<...>) named-group syntax in your regex—groups will then be accessible by meaningful names, in the match object's groupdict() output. Taken all together, that gives us:
PATTERNS = [ # a list of alternative acceptable formats
re.compile( r"""
^s* # beginning of string (optional whitespace)
(?P<degrees>d+)[s] # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
(?P<minutes>d+)' # integer number of minutes
(?P<seconds>d+(.d*)?)" # seconds, with optional decimal point and decimal places
(?P<axis>[NE]?) # optional 'N' or 'E' character (remove '?' to make it compulsory)
s*$ # end of string (optional whitespace)
""", re.VERBOSE ),
re.compile( r"""
^s* # beginning of string (optional whitespace)
(?P<degrees>d+)[s] # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
(?P<minutes>d+(.d*)?) # minutes, with optional decimal point and decimal places
(?P<axis>[NE]?) # optional 'N' or 'E' character (remove this line if this is never appropriate in this format)
s*$ # end of string (optional whitespace)
""", re.VERBOSE ),
]
edited Nov 19 '18 at 18:36
answered Nov 19 '18 at 18:15
jezjez
8,1702142
8,1702142
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379421%2fcheck-gps-string-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Punctuation should not be a problem. What did you try?
– usr2564301
Nov 19 '18 at 17:02
This will work:
[NE]$. If not, more details please.– usr2564301
Nov 19 '18 at 17:04
1
What exactly is the "correct" format? Do you expect values to be zero-padded? How many decimal places do you accept, and where? Are integers required to have zero'd decimal places? What values are acceptable for the cardinal direction at the end (
S,SW,SSW, etc...)?– Patrick Haugh
Nov 19 '18 at 17:04