# Import Files

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

```python
import pandas as pd
df = pd.read_csv("housing.csv")
```

Similarly the following syntax can be used for R

```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.

```python
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

```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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.zerve.ai/guide/notebook-view/files/import-files.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
