google polyline: string out of range - google-polyline

I try to use google polyline to decode the data. My data looks like
'yrwFpjpbMbDxB|BxAtBxAjFhDrFnDFHlBnAtBtAzBvAvFrDl#\\MfFNHo#~CtF|BXLn#aDp#}Cp#mCfAuD\{AlKmg##]L{#pAoGNup#bCoL#gBf#eC^gBb#cBjD{P\oBVyAFgA?cAs#_QK}#Q_AYcAa#cA]i#m#w#w#q#a#WuCyAcAk#}#o#aA{#}#}#aBuBG[m#m#oEuFcAwAmB}CeDaG_FcJqAeCm#oAKe#Yu#c#aBa#cB[eBEa#fCeBlCYzGq#ZzG'
If I put this string into the code
polyline.decode('yrwFpjpbMbDxB|BxAtBxAjFhDrFnDFHlBnAtBtAzBvAvFrDl#\\MfFNHo#~CtF|BXLn#aDp#}Cp#mCfAuD\{AlKmg##]L{#pAoGNup#bCoL#gBf#eC^gBb#cBjD{P\oBVyAFgA?cAs#_QK}#Q_AYcAa#cA]i#m#w#w#q#a#WuCyAcAk#}#o#aA{#}#}#aBuBG[m#m#oEuFcAwAmB}CeDaG_FcJqAeCm#oAKe#Yu#c#aBa#cB[eBEa#fCeBlCYzGq#ZzG'), it gives me the data.
However, if I run the program and save the string into a list, like code[],
and run the code
polyline.decode(code[0]), the error will say:
IndexError: string index out of range
I tried many times and cannot figure the problem.Can anybody tell me what I'm doing wrong? Thanks in advance!

It might be related to the escape characters in your string.
I a similar issue when copy/pasting a an encoded string from a web call into a python script as a raw string:
encoded_string = r"<pasted string>"
l = polyline.decode(encoded_string)
>>
>> .. IndexError: string index out of range
When making it a normal string (removing the 'r' qulification) it worked
encoded_string = "<pasted string>"
l = polyline.decode(encoded_string)

Related

How to parse a configuration file (kind of a CSV format) using LUA

I am using LUA on a small ESP8266 chip, trying to parse a text string that looks like the one below. I am very new at LUA, and tried many similar scripts found at this forum.
data="
-- String to be parsed\r\n
Tiempo1,20\r\n
Tiempo2a,900\r\n
Hora2b,27\r\n
Tiempo2b,20\r\n
Hora2c,29\r\n
Tiempo2c,18\r\n"
My goal would be to parse the string, and return all the configuration pairs (name/value).
If needed, I can modify the syntax of the config file because it is created by me.
I have been trying something like this:
var1,var2 = data:match("([Tiempo2a,]), ([^,]+)")
But it is returning nil,nil. I think I am on the very wrong way to do this.
Thank you very much for any help.
You need to use gmatch and parse the values excluding non-printable characters (\r\n) at the end of the line or use %d+
local data=[[
-- String to be parsed
Tiempo1,20
Tiempo2a,900
Hora2b,27
Tiempo2b,20
Hora2c,29
Tiempo2c,18]]
local t = {}
for k,v in data:gmatch("(%w-),([^%c]+)") do
t[#t+1] = { k, v }
print(k,v)
end

Python 3 String Formatting Issues

I have run into an issue where i can't format a string to be printed.
The function is suppossed to convert Binary into Text which is does brilliantly but the printed out result is formatted all the way the right and not the left.
I have tried resolving this by looking up how to format the string but im getting no luck. Im hoping someone can resolve this issue for me.
Heres the code:
elif Converter_Choice2 == str(3):
def Bin_to_Txt():
print("\nYour Message in Binary:")
bin_input = input("")
binary_int = int(bin_input, 2)
byte_number = binary_int.bit_length() + 7 // 8
binary_array = binary_int.to_bytes(byte_number, "big")
ascii_text = binary_array.decode()
clear()
print("\nYour Message in Text:")
print(ascii_text)
Bin_to_Txt()
I tried different ways to format it but im still new to Python 3. I tried putting "ascii_text" into another string to format it, so i could print that string but it didn't work.
ascii_text_formatted = ("{:<15}".format(ascii_text))
print(ascii_text_formatted)
Some advice for this would be great.
Heres a quick Binary code that can be used: 0100100001100101011011000110110001101111
The decoded version should say "Hello".
I managed to find the answer. If anyone else has this issue or something similar try this:
The issue was the variable "binary_array" was printing out invisible numbers before the printed answer in this case "Hello". Due to this it would print "Hello" all the way to the right as the invisible numbers where in front of it.
To fix this issue i added [34:] at the end of the "binary_array" string to remove those invisible numbers from the print. By adding [34:] it means the first 34 characters/numbers wont be printed even if they are invisible. So this can be any number that you need it to be. For example if i changed 34 to 35 it would remove the "H" from "Hello" and print "ello".
Heres some screenshots of the function block and printed responces from before and after adding [34:].
https://imgur.com/a/W25G1FZ

Parse a line to get specific string in python

I'm very new for python and tried to get parse the URL from the line. How can I get the line?
application_url: https://hafaf.daff.io
I tried to use split but I could not get.
So split works as such:
mystring = "Hello, my name is Sam!"
print(mystring.split('Hello')[1])
That will output:
", my name is Sam!"
What split does it quite literally as it sounds like, is split on a specific string or character.
So to get the url there you'd do the following:
my_url = "application_url: https://hafaf.daff.io".split("application_url: ")[1]
Which would result in the variable my_url being "https://hafaf.daff.io"
Do note the inclusion of the spaces when splitting.
Split breaks a string into a LIST object which you can then access by index. So when I go to get your url from that string I search for the second index being 1 as the "application_url: " is in position 0.

Python replace characters in string

Maybe I have trival issue but I can't find solution.
I use Raspberry Pi to read value from serial port. Input from serial looks like " b'1\r\n' ".
In this input I need only the number. I tried this code:
data = str(data)
data = data[2:7]
data = data.replace("\r\n","")
print(data)
Output of this code is : "1\r\n". I can't get rid of this part of string, replace function just doesn't work and I don't know why.
you have bytes you can use the decode method of bytes to get back a string. you can then use rstrip method of str to remove the trailing new line chars.
data = b'1\r\n'
print(data)
data = data.decode(('utf-8;')).rstrip()
print(data)
OUTPUT
b'1\r\n'
1

Unable to remove string from text I am extracting from html

I am trying to extract the main article from a web page. I can accomplish the main text extraction using Python's readability module. However the text I get back often contains several &#13 strings (there is a ; at the end of this string but this editor won't allow the full string to be entered (strange!)). I have tried using the python replace function, I have also tried using regular expression's replace function, I have also tried using the unicode encode and decode functions. None of these approaches have worked. For the replace and Regular Expression approaches I just get back my original text with the &#13 strings still present and with the unicode encode decode approach I get back the error message:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position 2099: ordinal not in range(128)
Here is the code I am using that takes the initial URL and using readability extracts the main article. I have left in all my commented out code that corresponds to the different approaches I have tried to remove the 
 string. It appears as though &#13 is interpreted to be u'\xa9'.
from readability.readability import Document
def find_main_article_text_2():
#url = 'http://finance.yahoo.com/news/questcor-pharmaceuticals-closes-transaction-acquire-130000695.html'
url = "http://us.rd.yahoo.com/finance/industry/news/latestnews/*http://us.rd.yahoo.com/finance/external/cbsm/SIG=11iiumket/*http://www.marketwatch.com/News/Story/Story.aspx?guid=4D9D3170-CE63-4570-B95B-9B16ABD0391C&siteid=yhoof2"
html = urllib.urlopen(url).read()
readable_article = Document(html).summary()
readable_title = Document(html).short_title()
#readable_article.replace("u'\xa9'"," ")
#print re.sub("
",'',readable_article)
#unicodedata.normalize('NFKD', readable_article).encode('ascii','ignore')
print readable_article
#print readable_article.decode('latin9').encode('utf8'),
print "There are " ,readable_article.count("
"),"
's"
#print readable_article.encode( sys.stdout.encoding , '' )
#sent_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
#sents = sent_tokenizer.tokenize(readable_article)
#new_sents = []
#for sent in sents:
# unicode_sent = sent.decode('utf-8')
# s1 = unicode_sent.encode('ascii', 'ignore')
#s2 = s1.replace("\n","")
# new_sents.append(s1)
#print new_sents
# u'\xa9'
I have a URL that I have been testing the code with included inside the def. If anybody has any ideas on how to remove this &#13 I would appreciate the help. Thanks, George

Resources