‘str’ object has no attribute ‘decode’ in Python3

One encodes strings, and one decodes bytes. You should read bytes from the file and decode them: for lines in open(‘file’,’rb’): decodedLine = lines.decode(‘ISO-8859-1’) line = decodedLine.split(‘\t’) Luckily open has an encoding argument which makes this easy: for decodedLine in open(‘file’, ‘r’, encoding=’ISO-8859-1′): line = decodedLine.split(‘\t’)

Writing to CSV with Python adds blank lines [duplicate]

The way you use the csv module changed in Python 3 in several respects (docs), at least with respect to how you need to open the file. Anyway, something like import csv with open(‘test.csv’, ‘w’, newline=””) as fp: a = csv.writer(fp, delimiter=”,”) data = [[‘Me’, ‘You’], [‘293’, ‘219’], [’54’, ’13’]] a.writerows(data) should work.

Return in generator together with yield

This is a new feature in Python 3.3 (as a comment notes, it doesn’t even work in 3.2). Much like return in a generator has long been equivalent to raise StopIteration(), return <something> in a generator is now equivalent to raise StopIteration(<something>). For that reason, the exception you’re seeing should be printed as StopIteration: 3, … Read more

How to install pip for Python 3 on Mac OS X?

UPDATE: This is no longer necessary with Python3.4. It installs pip3 as part of the stock install. I ended up posting this same question on the python mailing list, and got the following answer: # download and install setuptools curl -O https://bootstrap.pypa.io/ez_setup.py python3 ez_setup.py # download and install pip curl -O https://bootstrap.pypa.io/get-pip.py python3 get-pip.py Which … Read more

Import arbitrary python source file. (Python 3.3+)

Found a solution from importlib test code. Using importlib.machinery.SourceFileLoader: >>> import importlib.machinery >>> loader = importlib.machinery.SourceFileLoader(‘a_b’, ‘/tmp/a-b.txt’) >>> mod = loader.load_module() >>> mod <module ‘a_b’ from ‘/tmp/a-b.txt’> NOTE: only works in Python 3.3+. UPDATE Loader.load_module is deprecated since Python 3.4. Use Loader.exec_module instead: >>> import types >>> import importlib.machinery >>> loader = importlib.machinery.SourceFileLoader(‘a_b’, ‘/tmp/a-b.txt’) >>> … Read more

Python 3: ImportError “No Module named Setuptools”

Your setup.py file needs setuptools. Some Python packages used to use distutils for distribution, but most now use setuptools, a more complete package. Here is a question about the differences between them. To install setuptools on Debian: sudo apt-get install python3-setuptools For an older version of Python (Python 2.x): sudo apt-get install python-setuptools

How to make program go back to the top of the code instead of closing [duplicate]

Use an infinite loop: while True: print(‘Hello world!’) This certainly can apply to your start() function as well; you can exit the loop with either break, or use return to exit the function altogether, which also terminates the loop: def start(): print (“Welcome to the converter toolkit made by Alan.”) while True: op = input … Read more