Feed python with read line in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Feed python with read line in shell
# 8  
Old 04-03-2019
Frankly speaking, baris35,

The information you are pulling from that movie review site is a JSON object.

The great majority of web developers process JSON objects with JSON object processing tools.

For example, this is tutorial for parsing JSON when using Python:

Python JSON

Reference: Parse JSON - Convert from JSON to Python

Quote:
If you have a JSON string, you can parse it by using the json.loads() method.

The result will be a Python dictionary.

Example
Convert from JSON to Python:
Code:
import json

# some JSON:
x =  '{ "name":"John", "age":30, "city":"New York"}'

# parse x:
y = json.loads(x)

# the result is a Python dictionary:
print(y["age"])

This User Gave Thanks to Neo For This Post:
# 9  
Old 04-03-2019
And here is a more complete Python reference for processing JSON, in general:

Current version as of today:

json - JSON encoder and decoder - Python 3.7.3 documentation
# 10  
Old 04-03-2019
If you need only one keyword per script run, try (given your shell provides "here strings")

Code:
while read KW; do python test.py  <<< "$KW"; done < keywords

This User Gave Thanks to RudiC For This Post:
# 11  
Old 04-03-2019
I prefer, generally, Javascript to Python, but FYI, here is an example Python program to read a JSON object via a URL using urllib2 and simplejson.

There are many ways to to this, this is only one quick example:

Code:
import urllib2
import simplejson
 
response = urllib2.urlopen("https://myurl/custom/get/")
data = simplejson.load(response)
print data
# => {'content': 'Hello World!', 'success': True}

If you want to get the value of a specific key in the JSON object:

Code:
import urllib2
import simplejson
 
response = urllib2.urlopen("https://myurl/custom/get/")
data = simplejson.load(response)
print data["content"]
# => Hello World!

These kind of web-based JSON operations are nearly trivial to do in Python and Javascript.
This User Gave Thanks to Neo For This Post:
# 12  
Old 04-03-2019
Thank you Rudic & Neo,
Code:
while read KW; do python test.py  <<< "$KW"; done < keywords

It's very good. One line and that's it Smilie

Dear Neo,
I need more time to read and understand.

Thank you
Boris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

2. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

3. Shell Programming and Scripting

Want to remove a line feed depending on number of tabs in a line

Hi! I have been struggling with a large file that has stray end of line characters. I am working on a Mac (Lion). I mention this only because I have been mucking around with fixing my problem using sed, and I have learned far more than I wanted to know about Unix and Mac eol characters. I... (1 Reply)
Discussion started by: user999991
1 Replies

4. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

5. Shell Programming and Scripting

How to skip the first line of the script in shell using python?

How to skip first line of the script in shell, using python. (3 Replies)
Discussion started by: KarthikPS
3 Replies

6. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

7. Shell Programming and Scripting

Unix, awk to read .ksh feed

I need some advice, I have live feed containing xml messages which means there is new messages every minute. I need a script that will run every 2 hours using the current time minus 2 hours ( which I able to do) However I have problem with the date formatting i.e. One date is... (3 Replies)
Discussion started by: INHF
3 Replies

8. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

9. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

10. Shell Programming and Scripting

replace last form feed with line feed

Hi I have a file with lots of line feeds and form feeds (page break). Need to replace last occurrence of form feed (created by - echo "\f" ) in the file with line feed. Please advise how can i achieve this. TIA Prvn (5 Replies)
Discussion started by: prvnrk
5 Replies
Login or Register to Ask a Question