How to read a csv file data from one drive using python?

magnet 0 Reputation points
2024-03-21T09:45:01.3166667+00:00
import requests
import json
import csv
import pandas as pd

# Replace 'YOUR_ACCESS_TOKEN' and 'YOUR_FILE_ID' with appropriate values
access_token = "insert token"



file_id = 'your_file_id'


# Define the URL to fetch the file content from OneDrive
url = f'https://graph.microsoft.com/v1.0/me/drive/items/{file_id}/children'

# Set authorization headers
headers = {
    'Authorization': 'Bearer ' + access_token
}

# Fetch file content from OneDrive
response = requests.get(url, headers=headers)
# print("response : ", response)

# Check if request was successful
if response.status_code == 200:
    # Read CSV data from response content
    csv_data = response.text.splitlines()
    print("csv_data: ", csv_data)
    # data = csv_data[0].webUrl
    data = csv_data[0]
    newData = json.loads(data)
    print("newData : ", newData['value'][0]['webUrl'])
    csv_data_cleaned = newData['value'][0]['webUrl']
    if csv_data_cleaned:
        # df = pd.DataFrame(csv_data_cleaned)
        # print("df  data :", df)
        url_response = requests.get(csv_data_cleaned, headers=headers)
        print("url_response :", url_response)
    # print("data : ", newData['webUrl'])
    # print("csv_data_cleaned : ", csv_data_cleaned['webUrl'])
    # if newData["webUrl"]:
    #     df = pd.read_csv(newData["webUrl"] + "/Iris.csv")
    #     print("df data: ", df)

    # Parse CSV data using csv.reader
    # reader = csv.reader(csv_data)
    # for row in reader:
    #     print("row :",row)
else:
    print("Error:", response.status_code, response.text)


trying to read from this code

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,029 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.