简体   繁体   中英

Error with requests module while converting CSV file to URL for an API in Python

I'm new on Python.

I'm looking for a way to convert a CSV file into a URL.

I have a CSV file: twitter_comment.csv and I am looking for a way to use it as a URL like this:

http://localhost:8080/twitter.

The url http://localhost:8080/twitter would contain the twitter_comment.csv file.

I have looked on the Internet and i didn't find with I was looking for.

I tried to do the following...

import requests
response = requests.get("twitter_comment.csv")

I got this error.

response = requests.get("twitter_comment.csv")
AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)

Do you think it is possible?

Thanks.

import requests

serverSite = 'http://localhost:8080/twitter/'
fileName = 'twitter_comment.csv'
url = serverSite + fileName
response = requests.get(url)

I think the problem is that you named a file requests.py , located in the same directory. Try renaming the file.

It looks like this, but it's not fully working. Right now, the url is mixed with the data. I tried to do a request mapping.

import requests
import csv 
import json

from django_request_mapping import request_mapping


jsonArray = []
serverSite = 'http://localhost:8000/' # open cmd and enter python -m http.server

#@request_mapping("twitter")
#def data(self, request):
#    jsonString = request.GET
#    return HttpResponse("ok")

#read csv file
with open(r'C:\Users\nom\Documents\IPSSI\twitter_comment.csv') as csvf: 
    #load csv file data using csv library's dictionary reader
    csvReader = csv.DictReader(csvf)

    #convert each csv row into python dict
    for row in csvReader: 
        #add this python dict to json array
        jsonArray.append(row)

#convert python jsonArray to JSON String and write to file
with open(r'data.json', 'w', encoding='utf-8') as jsonf: 
    jsonString = json.dumps(jsonArray, indent=4)
    jsonf.write(jsonString)

url = serverSite + jsonString
print(url)
response = requests.get(url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM