'Parsing gerrit query json output in python (multiple entries)
I am running a gerrit query with a regex in "intopic" via python. This basically returns multiple gerrit reviews.
Here is the script snippet:
cmd_string="ssh -p 29418 username@my_gerrit_server.net gerrit query --current-patch-set --format=JSON branch:master intopic:{my_reviews_*}"
child = pexpect.spawn(cmd_string)
child.delaybeforesend = 1
child.expect(r'Enter passphrase for key(.*?):', timeout=10)
child.sendline(passwd)
child.expect (r'(.*?)$')
cmd_op = child.read(size=-1)
json_dict = json.loads(cmd_op)
The step at json.loads fails. The cmd_op looks something like this:
{"project":"prj1","branch":"master".... , "subject":"review1",... }
{"project":"prj2","branch":"master".... , "subject":"review2",... }
{"type":"stats","rowCount":2,"runTimeMilliseconds":31,"moreChanges":false}
I am looking to read some value from each of the above review entry to do some further processing
This doesnt look like a valid json format as entries are not comma separated. Is there any alternate way i could fetch/process this data?
Running on Ubuntu with Python 2.7.12
Thanks in advance,
Solution 1:[1]
First of all, there are some minor errors in the code.
- Inlcude necessary headers.
- Use std:: namespace prefix.
#include <iostream>
#include <string>
#include <cctype>
void convert(std::string &s){
for (int i =0; i < s.length(); i++){
s[i] = std::toupper(s[i]);
}
}
int main(){
std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
convert(name);
std::cout << name << std::endl;
return 0;
}
Using pointer version?
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#define SIZE 100
void convert(char *s, size_t size){
for (int i =0; i < size; i++){
s[i] = std::toupper(s[i]);
}
}
int main(){
char name[SIZE];
std::cout << "Enter your name: ";
std::fgets(name, SIZE, stdin);
convert(name, strlen(name));
std::cout << name << std::endl;
return 0;
}
Another version using pointers
#include <iostream>
#include <string>
#include <cctype>
void convert(std::string *s){
for (int i =0; i < s -> length(); i++){
(*s)[i] = std::toupper((*s)[i]);
}
}
int main(){
std::string *name = new std::string();
std::cout << "Enter your name: ";
std::getline(std::cin, *name);
convert(name);
std::cout << *name << std::endl;
delete name;
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
