Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an option to only select required fields #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions json_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ def reduce_item(key, value):


if __name__ == "__main__":
if len(sys.argv) != 4:
if len(sys.argv) < 4:
print ("\nUsage: python json_to_csv.py <node> <json_in_file_path> <csv_out_file_path>\n")
print('Usage with limited fields: python json_to_csv.py <node> <json_in_file_path> <csv_out_file_path> <field1> <field2>')
else:
#Reading arguments
node = sys.argv[1]
Expand All @@ -79,7 +80,22 @@ def reduce_item(key, value):

processed_data = []
header = []

acceptable_keys = []
#reading the field that needs to be saved
if len(sys.argv) > 4:
for i in range(4,len(sys.argv)):
acceptable_keys.append(sys.argv[i])
print('Only copying from: ',end='')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is throwing syntax error.

Copy link
Author

@varnitsingh varnitsingh Oct 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try removing end=''. See if that works. There is a issue sometimes with the python linter.

print(acceptable_keys)

for item in data_to_be_processed:
if len(sys.argv) > 4:
all_keys = list(item.keys())
delete_keys = list(set(all_keys)-set(acceptable_keys))
for key in delete_keys: #delete the keys not mentioned in command line
del item[key]

reduced_item = {}
reduce_item(node, item)

Expand All @@ -96,4 +112,4 @@ def reduce_item(key, value):
for row in processed_data:
writer.writerow(row)

print ("Just completed writing csv file with %d columns" % len(header))
print ("Just completed writing csv file with %d columns" % len(header))