with open(“C:\Users\oliver.r.caluag\Desktop\capture_for_Oliver.txt”) as file:
data = json.load(file)
fname = “oli.csv”
with open(fname, “w”) as file:
csv_file = csv.writer(file)
csv_file.writerow([“Jitter”, “Latency”, “Link”, “Packetloss”, “Timestamp”])
for result in data[“result”]:
for data_item in result[“data”]:
for result in data_item[“response”][“results”]:
for item in result[“logs”]:
csv_file.writerow([item[“jitter”], item[“latency”], item[“link”], item[“packetloss”], item[“timestamp”]])
error received:
File “C:\Users\oliver.r.caluag\PycharmProjects\pythonProject\venv\Myjson1.py”, line 9
with open(fname, “w”) as file:
^
SyntaxError: invalid character ‘“’ (U+201C)
I explored a bit and add r to line 3
with open(r"C:\Users\oliver.r.caluag\Desktop\capture_for_Oliver.txt") as file:
data = json.load(file)
fname = “oli.csv”
now I received this error:
File “C:\Users\oliver.r.caluag\PycharmProjects\pythonProject\venv\Myjson1.py”, line 9
with open(fname, “w”) as file:
^
SyntaxError: invalid character ‘“’ (U+201C)
Orig. code was created in Mac, does this have connection to the error messages that I have received now that I run it in Windows?
Python strings are denoted with either the ascii single quote (’) or the ascii double quote ("). Your code has something like a “smart quote” instead (the unicode LEFT DOUBLE QUOTATION MARK), which is not valid as a string indication.
I suspect you have used an editor or some process that tried to change the quote for you. Replace those symbols with plain quotes and you should be good.
I just rewrite my code in my other laptop with Windows OS. It looks fine now, however, i received new error message after executing the last line.
import json
import csv
with open(“C:/Users/oliver.r.caluag/Desktop/capture_for_Oliver.txt”) as file:
data = json.load(file)
fname = “oli.csv”
with open(fname, “w”) as file:
csv_file = csv.writer(file)
csv_file.writerow([“Jitter”, “Latency”, “Packetloss”, “Timestamp”])
for result in data[“result”]:
for data_item in result[“data”]:
for result in data_item[“response”][“results”]:
for item in result[“logs”]:
csv_file.writerow([item[“jitter”],item[“latency”],item[“link”],item[“packetloss”],item[“timestamp”]])
error message:
File "C:\Users\oliver.r.caluag\PycharmProjects\pythonProject\venv\Myjson1.py", line 16, in <module>
csv_file.writerow([item["jitter"],item["latency"],item["link"],item["packetloss"],item["timestamp"]])
means that you are trying to read or write to a file after it is closed.
It looks like the problem is that you do this (comments inserted):
with open(fname, "w") as file:
# Inside this block, the file is open.
csv_file = csv.writer(file)
csv_file.writerow(["Jitter", "Latency", "Packetloss", "Timestamp"])
# At the end of the block, the file is automatically closed.
for result in data["result"]:
for data_item in result["data"]:
for result in data_item["response"]["results"]:
for item in result["logs"]:
# Attempt to write data to a closed file.
csv_file.writerow( ... )
All your code that writes to the file needs to be fully indented into
the block under the with. If you fix the indentation, it should
(hopefully!) work as expected.
Thank you everyone who assisted me with my concerns. I learned a lot and glad to say that I have already achieved of my expectation.
You guys rock! thank you!