👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I am trying to read in a JSON file into Python pandas (0.14.0) data frame. Here is the first line line of the JSON file:
{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "P_Mk0ygOilLJo4_WEvabAA", "review_id": "OeT5kgUOe3vcN7H6ImVmZQ", "stars": 3, "date": "2005-08-26", "text": "This is a pretty typical cafe. The sandwiches and wraps are good but a little overpriced and the food items are the same. The chicken caesar salad wrap is my favorite here but everything else is pretty much par for the course.", "type": "review", "business_id": "Jp9svt7sRT4zwdbzQ8KQmw"}
I am trying do the following:df = pd.read_json(path)
.
I am getting the following error (with full traceback):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/d/anaconda/lib/python2.7/site-packages/pandas/io/json.py", line 198, in read_json
date_unit).parse()
File "/Users/d/anaconda/lib/python2.7/site-packages/pandas/io/json.py", line 266, in parse
self._parse_no_numpy()
File "/Users/d/anaconda/lib/python2.7/site-packages/pandas/io/json.py", line 483, in _parse_no_numpy
loads(json, precise_float=self.precise_float), dtype=None)
ValueError: Trailing data
What is the Trailing data
error? How do I read it into a data frame?
Following some suggestions, here are few lines of the .json file:
{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "P_Mk0ygOilLJo4_WEvabAA", "review_id": "OeT5kgUOe3vcN7H6ImVmZQ", "stars": 3, "date": "2005-08-26", "text": "This is a pretty typical cafe. The sandwiches and wraps are good but a little overpriced and the food items are the same. The chicken caesar salad wrap is my favorite here but everything else is pretty much par for the course.", "type": "review", "business_id": "Jp9svt7sRT4zwdbzQ8KQmw"}
{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "TNJRTBrl0yjtpAACr1Bthg", "review_id": "qq3zF2dDUh3EjMDuKBqhEA", "stars": 3, "date": "2005-11-23", "text": "I agree with other reviewers - this is a pretty typical financial district cafe. However, they have fantastic pies. I ordered three pies for an office event (apple, pumpkin cheesecake, and pecan) - all were delicious, particularly the cheesecake. The sucker weighed in about 4 pounds - no joke.
No surprises on the cafe side - great pies and cakes from the catering business.", "type": "review", "business_id": "Jp9svt7sRT4zwdbzQ8KQmw"}
{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "H_mngeK3DmjlOu595zZMsA", "review_id": "i3eQTINJXe3WUmyIpvhE9w", "stars": 3, "date": "2005-11-23", "text": "Decent enough food, but very overpriced. Just a large soup is almost $5. Their specials are $6.50, and with an overpriced soda or juice, it"s approaching $10. A bit much for a cafe lunch!", "type": "review", "business_id": "Jp9svt7sRT4zwdbzQ8KQmw"}
This .json file I am using contains one JSON object in each line as per the specification.
I tried the jsonlint.com website as suggested and it gives the following error:
Parse error on line 14:
...t7sRT4zwdbzQ8KQmw"}{ "votes": {
----------------------^
Expecting "EOF", "}", ",", "]"
👻 Read also: what is the best laptop for engineering students?
Loading a file with more than one line of JSON into Pandas __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Loading a file with more than one line of JSON into Pandas __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from Loading a file with more than one line of JSON into Pandas, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Loading a file with more than one line of JSON into Pandas
- Deutsch Loading a file with more than one line of JSON into Pandas
- Français Loading a file with more than one line of JSON into Pandas
- Español Loading a file with more than one line of JSON into Pandas
- Türk Loading a file with more than one line of JSON into Pandas
- Русский Loading a file with more than one line of JSON into Pandas
- Português Loading a file with more than one line of JSON into Pandas
- Polski Loading a file with more than one line of JSON into Pandas
- Nederlandse Loading a file with more than one line of JSON into Pandas
- 中文 Loading a file with more than one line of JSON into Pandas
- 한국어 Loading a file with more than one line of JSON into Pandas
- 日本語 Loading a file with more than one line of JSON into Pandas
- हिन्दी Loading a file with more than one line of JSON into Pandas
California | 2023-03-29
Maybe there are another answers? What Loading a file with more than one line of JSON into Pandas exactly means?. Will use it in my bachelor thesis
Munchen | 2023-03-29
Simply put and clear. Thank you for sharing. Loading a file with more than one line of JSON into Pandas and other issues with Expect PHP module was always my weak point 😁. Will get back tomorrow with feedback
California | 2023-03-29
Simply put and clear. Thank you for sharing. Loading a file with more than one line of JSON into Pandas and other issues with Python functions was always my weak point 😁. Checked yesterday, it works!