CezarySzulc
CezarySzulc

Reputation: 1997

How do I assign the output of a command to a variable in Jupyter notebook?

Is there a way to assign a value to a variable, that value which we get in terminal by writing any command?

I try with this code:

!number_of_lines=$(wc -l < cord_19.json)
!echo $number_of_lines

I get empty output. When I run

!wc -l < cord_19.json

I get correct response with number.

Do you have idea what is wrong or how can I set variable?

Upvotes: 5

Views: 2579

Answers (1)

Marek Grzenkowicz
Marek Grzenkowicz

Reputation: 17333

Try:

number_of_lines = !wc -l < cord_19.json
!echo $number_of_lines

or:

number_of_lines = !wc -l < cord_19.json
print(number_of_lines)

See Pipe Ipython magic output to a variable? for related discussion.

Upvotes: 5

Related Questions