Split the results from urrlib - python

Need to import data from the ISS.
Use the code
r= urllib.request.urlopen('http://www.celestrak.com/NORAD/elements/stations.txt')
x=r.read(1000)
when I try to split the data with
x=x.split("\r\n")
I get the error
raceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
x=x.split("\r\n")
TypeError: a bytes-like object is required, not 'str'
How can I fix this?

Why not just use requests?
import requests
response = requests.get("http://www.celestrak.com/NORAD/elements/stations.txt")
text = response.text.split("\r\n")
for t in text:
print t

Related

Can not find a specific regular expressions

I am trying to scrap specific information from the site but I am receiving an error. Here is the simplified version of the main problem:
import re
b='href="/bp/vendor?vendorCodes=C901U">C901U</a></span></div></div></div><div heyaa'
c=re.search('href="/bp/vendor?vendorCodes=C901U">C901U</a></span></div></div></div><div',b)
If I try to find what is in c I receive this error:
c.group()
Traceback (most recent call last):
File "<pyshell#87>", line 1, in <module>
c.group()
AttributeError: 'NoneType' object has no attribute 'group'
Thanks in advance.

Not able to open and load pickle file in python 3.6.1

I am trying to open and load pickle file but by two ways. But every time I am getting an error.
Request you to please help.
First way :
enron_data = pickle.load(open("D:/New/ud120-projects/final_project/final_project_dataset.pkl", "r"))
Error: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
Second Way :
enron_data = pickle.load(open("D:/New/ud120-projects/final_project/final_project_dataset.pkl", "rb"))
Error : Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_pickle.UnpicklingError: the STRING opcode argument must be quoted
Request you to please help
If you are on Windows you have to use a raw string and backslashes like this:
r'D:\path\to\your\file'

Splitting strings unhashable type

I have never split strings in Python before so I am not too sure what is going wrong here.
import pyowm
owm = pyowm.OWM('####################')
location = owm.weather_at_place('Leicester, uk')
weather = location.get_weather()
weather.get_temperature('celsius')
temperature = weather.get_temperature('celsius')
print(temperature[5:10])
Error received
sudo python weather.py
Traceback (most recent call last):
File "weather.py", line 10, in <module>
print(temperature[5:10])
TypeError: unhashable type
get_temperature returns a dictionary, which you're then trying to index with a slice object, which is not hashable. e.g.
>>> hash(slice(5, 10))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type
To get the temperature, you need to get it from the dictionary like this:
temperature['temp']

Any ideas how to fix TypeError: 'str' does not support the buffer interface?

I keep getting this error "TypeError: 'str' does not support the buffer interface" Not sure what is going wrong. Any assistance would be great.
import zlib
#User input for sentnce & compression.
sentence = input("Enter the text you want to compress: ")
com = zlib.compress(sentence)
#Opening file to compress user input.
with open("listofwords.txt", "wb") as myfile:
myfile.write(com)
The error means that you are trying to pass str object (Unicode text) instead of binary data (byte sequence):
>>> import zlib
>>> zlib.compress('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface
Python 3.5 improves the error message here:
>>> import zlib
>>> zlib.compress('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
To save text as binary data, you could encode it using a character encoding. To compress the data, you could use gzip module:
import gzip
import io
with io.TextIOWrapper(gzip.open('sentence.txt.gz', 'wb'),
encoding='utf-8') as file:
print(sentence, file=file)

pickle, 'str' does not support buffer interface in python

Ok so I have the following code:
def rate_of_work(self):
global rate
rate = (turtle.textinput("Your Work Rate","What is your hourly work rate in US Dollars?"))
outFile_rate = pickle.dumps(rate)
rate1 = pickle.loads(rate)
rate2 = ((hours*rate1) + (minutes*rate1)*0.0167 + (seconds*rate1)*0.000278) #isnt necessary information
rate3 = round(rate2, 2) #isnt necessary information
im getting the error:
rate1 = pickle.loads(rate)
TypeError: 'str' does not support the buffer interface
please help
I assume you are working under python 3.x
For the specific error, pickle.loads() only accepts bytes, and you are trying to give a plain string to it, that's why it fails.
>>> pickle.loads("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface
>>> pickle.loads(b"")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError

Categories