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.

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

hash function in Python 3.3 returns different results between sessions

Python uses a random hash seed to prevent attackers from tar-pitting your application by sending you keys designed to collide. See the original vulnerability disclosure. By offsetting the hash with a random seed (set once at startup) attackers can no longer predict what keys will collide. You can set a fixed seed or disable the … Read more

Why is dictionary ordering non-deterministic?

Update: In Python 3.6, dict has a new implementation which preserves insertion order. From Python 3.7, this order-preserving behaviour is guaranteed: the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec. This is the result of a security fix from 2012, which was enabled by … Read more

Python, split user input twice. At pair and space

Basic String manipulation. There are lots of tutorials out there on that, go look them up. From your question, it looks like you would want to use the isalpha() builtin. Here’s a function that should do the string manipulation like you said. def pair(user): user=user.split(” “) for x in range(len(user)): print (“\nPair part “+str(x)+”:”) for … Read more