Python syntax error in version 2.6

452 Views Asked by At

I'm not very familiar with python but I need to fix a script which throws a syntax error in version 2.6. Can anybody help to explain the problem?

import pandas as pd
....
d = pd.read_csv(csv_filename, skiprows=skip).to_dict()
d = {k: d[k].values() for k in d}

This is the error message:

d = {k: d[k].values() for k in d}
                            ^
SyntaxError: invalid syntax
1

There are 1 best solutions below

1
jonrsharpe On BEST ANSWER

Dictionary comprehensions were a new feature in 2.7, and aren't valid syntax in earlier versions. Instead, pass dict a generator expression of two-tuples:

d = dict((k, d[k].values()) for k in d)