> For the complete documentation index, see [llms.txt](https://docs.zerve.ai/guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zerve.ai/guide/canvas-view/files/import-files.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
