gvalkov / python-oneliner

Module for practical Python one-liners
Other
37 stars 7 forks source link

Can't get simple multi-statement processing to work #4

Closed anjishnu closed 8 years ago

anjishnu commented 9 years ago

I want to do something like:

for line in fileinput.input():
 j = json.loads(line)
 j['a'] = j['b'']
 print(json.dumps(j))

I tried statements like

$ cat file.txt | python -m oneliner -m json -pe "j = json.loads(line); j['a'] = j['b']; print(json.dumps(j))"

But they all failed.

gvalkov commented 9 years ago

Hi, can you try with the -ns flags? I got it working with:

cat <<EOF | python -m oneliner -m json -ns 'j=json.loads(line); j["a"], j["b"] = j["b"], j["a"]; print(json.dumps(j));' 
{"a": 1, "b": 2}
{"a": 3, "b": 4}
EOF
{"a": 2, "b": 1}
{"a": 4, "b": 3}
anjishnu commented 8 years ago

That worked, thanks!