How to fix ValueError: Trailing data in Python?

ValueError: Trailing data occurs when extra data is present after the expected data in a Python program. To fix this, the extra data must be removed or the program must be adjusted to process the extra data. It’s important to check the code for syntax errors and look for incorrect data types or extra characters in the data that could cause the error. Additionally, checking the data for consistency and verifying that the data is of the expected format can help resolve this issue.


One error you may encounter when using Python is:

ValueError: Trailing data

This error usually occurs when you attempt to import a JSON file into a pandas DataFrame, yet the data is written in lines separated by endlines like ‘n‘.

The easiest way to fix this error is to simply specify lines=True when importing the data:

df = pd.read_json('my_data.json', lines=True)

The following example shows how to fix this error in practice.

How to Reproduce the Error

Suppose we have the following JSON file:

Now suppose we attempt to import this JSON file into a pandas DataFrame:

#attempt to import JSON file into pandas DataFrame
df = pd.read_json('Documents/DataFiles/my_data.json')

ValueError: Trailing data

We receive an error because the “Review” item in our JSON file contains n to represent endlines.

How to Fix the Error

The easiest way to fix this error is to simply specify lines=True when importing the data:

#import JSON file into pandas DataFrame
df = pd.read_json('Documents/DataFiles/my_data.json', lines=True)

#view DataFrame
df

	ID	Rating	Review
0	A	8	Great movie.nI would recommend it.
1	B	5	Mediocre movie.nWould not recommend it.
2	C	3	Bad movie.nI would not recommend.
3	D	7	Decent movie.nI might recommend it.

Notice that we’re able to successfully import the JSON file into a pandas DataFrame without any errors.

If we’d like to remove the n endlines from the “Review” column, we can use the following syntax:

#replace n with empty space in 'Review' column
df['Review'] = df['Review'].str.replace('n', ' ')

#view updated DataFrame
df

	ID	Rating	Review
0	A	8	Great movie. I would recommend it.
1	B	5	Mediocre movie. Would not recommend it.
2	C	3	Bad movie. I would not recommend.
3	D	7	Decent movie. I might recommend it.

The n values are now removed from the “Review” column.

x