How to convert a string to utf-8 in Python

In Python 2 >>> plain_string = “Hi!” >>> unicode_string = u”Hi!” >>> type(plain_string), type(unicode_string) (<type ‘str’>, <type ‘unicode’>) ^ This is the difference between a byte string (plain_string) and a unicode string. >>> s = “Hello!” >>> u = unicode(s, “utf-8”) ^ Converting to unicode and specifying the encoding. In Python 3 All strings are … Read more

Writing a Python list of lists to a csv file

Python’s built-in CSV module can handle this easily: import csv with open(“output.csv”, “wb”) as f: writer = csv.writer(f) writer.writerows(a) This assumes your list is defined as a, as it is in your question. You can tweak the exact format of the output CSV via the various optional parameters to csv.writer() as documented in the library … Read more

Python UTC datetime object’s ISO format doesn’t include Z (Zulu or Zero offset)

Option: isoformat() Python’s datetime does not support the military timezone suffixes like ‘Z’ suffix for UTC. The following simple string replacement does the trick: In [1]: import datetime In [2]: d = datetime.datetime(2014, 12, 10, 12, 0, 0) In [3]: str(d).replace(‘+00:00’, ‘Z’) Out[3]: ‘2014-12-10 12:00:00Z’ str(d) is essentially the same as d.isoformat(sep=’ ‘) See: Datetime, … Read more

What is a good practice to check if an environmental variable exists or not?

Use the first; it directly tries to check if something is defined in environ. Though the second form works equally well, it’s lacking semantically since you get a value back if it exists and only use it for a comparison. You’re trying to see if something is present in environ, why would you get just … Read more

sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)

This might happen inside scikit, and it depends on what you’re doing. I recommend reading the documentation for the functions you’re using. You might be using one which depends e.g. on your matrix being positive definite and not fulfilling that criteria. EDIT: How could I miss that: np.isnan(mat.any()) #and gets False np.isfinite(mat.all()) #and gets True … Read more

Why isn’t my Pandas ‘apply’ function referencing multiple columns working? [closed]

Seems you forgot the ” of your string. In [43]: df[‘Value’] = df.apply(lambda row: my_test(row[‘a’], row[‘c’]), axis=1) In [44]: df Out[44]: a b c Value 0 -1.674308 foo 0.343801 0.044698 1 -2.163236 bar -2.046438 -0.116798 2 -0.199115 foo -0.458050 -0.199115 3 0.918646 bar -0.007185 -0.001006 4 1.336830 foo 0.534292 0.268245 5 0.976844 bar -0.773630 -0.570417 … Read more

How to display pandas DataFrame of floats using a format string for columns?

import pandas as pd pd.options.display.float_format=”${:,.2f}”.format df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890], index=[‘foo’,’bar’,’baz’,’quux’], columns=[‘cost’]) print(df) yields cost foo $123.46 bar $234.57 baz $345.68 quux $456.79 but this only works if you want every float to be formatted with a dollar sign. Otherwise, if you want dollar formatting for some floats only, then I think you’ll have … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)