I am not a programmer and have no experience with Pyton, so I would really appreciate any help to solve the few remaining issues I’ll explain bellow:
What I am trying to do is collect a few but same values from many .json files and get them into a spreadsheet to work with later.
So far I can get the values I need from all JSON files in a folder, but I would also need the JSON filename and the resulting CSV is not written, I get an error.
my python file:
import json
import glob
from datetime import datetime
import csv
src = "data/"
date = datetime.now()
data = []
files = glob.glob('data/*', recursive=True)
for single_file in files:
with open(single_file, 'r') as f:
try:
json_file = json.load(f)
data.append([
json_file['score'],
json_file['otherValue']
])
except KeyError:
print(f'Skipping {single_file}')
data.sort()
data.insert(0, ['fileName','score','otherValue'])
print(data)
#printing works
# Export to CSV.
csv_filename = f'{str(date)}.csv'
with open(csv_filename, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(data)
print("Updated CSV")
Thank you in advance for any help!
I think I am not far from this result just need some guidance by somebody who knows what they are doing.
You are iterating the file names using a loop so you need to add the file name to the every item of your data list inside the loop (inside data.append()).
The glob.glob() function returns list of file names. Your for loop iterates this list so in every iteration single_file contains the file name which is currently being processed.
I am glad that you were able to make the progress!
It looks like you are running the program on Windows This system has serious limitations regarding allowed characters in file names. I think here the problem is the colon : character.
I can easily, with a bit of text replacing in a text editor get that to be like
fileName, scoreMax, 5360, 835860, 5361, 1296420, 5362, 837220, 5363, 781170, 5364, 947670
This is the data I would need, so good so far.
The only thing missing is a line break after every file’s data, so when importing it into spreadsheet it becomes a table.
How could I add a line break after every file’s data added?
If you want to use date+time anyway - I for example prefer to follow ISO 8601 when possible. You can do it by omitting the separators. Using your stored date:
date.strftime('%Y%m%dT%H%M%S')
What is writer? Is not it csv.writer? Import just csv not from csv import writer. Your program will be easier to understand,
I have never used csv.writer but I suppose the writerow method should be used to write a single row while you feed it with all the data. I think you need to iterate the items (rows) of your data using for and call writerow for every row individually.
Look into the documentation to know how it should be used.