Python Requests getting (‘Connection aborted.’, BadStatusLine(“””,)) error

The error you get indicates the host isn’t responding in the expected manner. In this case, it’s because it detects that you’re trying to scrape it and deliberately disconnecting you. If you try your requests code with this URL from a test website: http://mirror.internode.on.net/pub/test/5meg.test1, you’ll see that it downloads normally. To get around this, fake … Read more

Why is the range object “not an iterator”? [duplicate]

The range object is iterable. However, it’s not an iterator. To get an iterator, you need to call iter() first: >>> r=range(5,15) >>> next(iter(r)) 5 >>> next(iter(r)) 5 >>> next(iter(r)) 5 >>> next(iter(r)) 5 >>> i=iter(r) >>> next(i) 5 >>> next(i) 6 >>> next(i) 7 >>> next(i) 8 >>> iter(r) <range_iterator object at 0x10b0f0630> >>> … Read more

check for file existence in Python 3 [duplicate]

if the file is not a file :(return False) import os.path if not os.path.isFile(file_name): print(“The File s% it’s not created “%file_name) os.touch(file_name) print(“The file s% has been Created …”%file_name) And you can write a simple code based on (try,Except): try: my_file = open(file_name) except IOError: os.touch(file_name)

Writing text to gzip file

mode=”wb” When writing to a file opened in binary mode, you must write bytes, not string. Encode your string using str.encode: with gzip.open(‘file.gz’, ‘wb’) as f: f.write(‘Hello world!’.encode()) mode=”wt” (found by OP) Alternatively, you can write strings to your file when you open it in the wt (explicit text) mode: with gzip.open(‘file.gz’, ‘wt’) as f: … Read more

update dictionary with dynamic keys and values in python

Remove the following line: mydic = {i : o[“name”]} and add the following before your loop: mydic = {} Otherwise you’re creating a brand new one-element dictionary on every iteration. Also, the following: mydic.update({i : o[“name”]}) is more concisely written as mydic[i] = o[“name”] Finally, note that the entire loop can be rewritten as a … Read more

Why doesn’t str.translate work in Python 3?

The keys used are the ordinals of the characters, not the characters themselves: ‘a’.translate({ord(‘a’): ‘b’}) It’s easier to use str.maketrans >>> ‘a’.translate(str.maketrans(‘a’, ‘b’)) ‘b’ >>> help(str.translate) Help on method_descriptor: translate(…) S.translate(table) -> str Return a copy of the string S, where all characters have been mapped through the given translation table, which must be a … Read more

Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”: http://landinghub.visualstudio.com/visual-cpp-build-tools [duplicate]

To solve any of the following errors: “Failed building wheel for misaka” “Failed to build misaka” “Microsoft Visual C++ 14.0 is required” “Unable to find vcvarsall.bat” The solution is: Go to: https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017 Select the free download under Visual Studio Community 2017 This will download the installer. Run the installer Now the most important step is … Read more