Import Files
Once the file is uploaded, it can be accessed in the Python or R blocks, and the files can be of any type.
Below are a couple of examples of accessing files such as a CSV, and JSON in the Python and R blocks
CSV files
CSV files can be imported into Python block by using the syntax below as dataframe. This supports all the file types supported by pandas
import pandas as pd
df = pd.read_csv("housing.csv")
Similarly the following syntax can be used for R
file = "housing.csv"
df = read.csv(file)
Accessing JSON
JSON files can also be accessed from the file system in Python or R block.
import json
# Open and read the raw content of the file
with open('./testdata.json', 'r') as json_file:
content = json_file.read()
# Print the first few characters to understand the structure
print("First 500 characters of the file:")
print(content[:500]) # Adjust the number as needed for better understanding
# Check if the content contains multiple JSON objects
try:
data = json.loads(content)
print("Single JSON object loaded successfully.")
except json.JSONDecodeError as e:
print(f"JSONDecodeError: {e}")
# Possible handling for multiple JSON objects:
# Assuming the objects are newline-separated
segments = content.splitlines() # Split by lines (change the delimiter if needed)
parsed_data = []
for segment in segments:
if segment.strip(): # Skip empty lines
try:
parsed_data.append(json.loads(segment))
except json.JSONDecodeError as segment_error:
print(f"Failed to parse segment: {segment_error}")
print(f"Parsed {len(parsed_data)} JSON objects/segments successfully.")
# For demonstration purposes, let's print the parsed objects
for i, obj in enumerate(parsed_data):
print(f"Object {i+1}: {obj}")
Syntax to access json in R
# Load required package
library(jsonlite)
# Read the raw content of the file
file_path <- './testdata.json'
content <- readLines(file_path, warn = FALSE)
# Print the first few characters to understand the structure
cat("First 500 characters of the file:\n")
cat(substr(paste(content, collapse = "\n"), 1, 500), "\n\n")
# Attempt to parse the content as a single JSON object
try({
data <- fromJSON(paste(content, collapse = "\n"))
cat("Single JSON object loaded successfully.\n")
print(data)
}, silent = TRUE)
# Handling multiple JSON objects scenario
segments <- unlist(strsplit(paste(content, collapse = "\n"), "\n"))
parsed_data <- list()
for (i in seq_along(segments)) {
segment <- segments[i]
if (nchar(trimws(segment)) > 0) { # Skip empty lines
try({
parsed_data[[i]] <- fromJSON(segment)
}, silent = TRUE)
}
}
cat("Parsed", le
Last updated