0

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.

Maura Pintor
  • 198
  • 1
  • 14

3 Answers3

0
import requests

serverSite = 'http://localhost:8080/twitter/'
fileName = 'twitter_comment.csv'
url = serverSite + fileName
response = requests.get(url)
Wrench
  • 490
  • 4
  • 13
  • hello @Wrench, thanks for your reply. I have the same error than before : Traceback (most recent call last): File "c:\Users\nom\Downloads\requests2.py", line 1, in import requests File "c:\Users\nom\Downloads\requests.py", line 2, in response = requests.get("twitter_comment.csv") AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import) – textSolver34761 Feb 19 '21 at 13:20
  • You can follow this. I think you are importing wrong requests.py which does not have get method. https://stackoverflow.com/questions/12258816/module-object-has-no-attribute-get-python-error-requests – Wrench Feb 19 '21 at 13:44
  • Maura Pintor send me to a link where the guy did the same thing i did... i deleted the previsous files. and it worked... but i have a new error : requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /twitter/twitter_comment.csv (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] Aucune connexion n’a pu être établie car l’ordinateur cible l’a expressément refusée')) – textSolver34761 Feb 19 '21 at 13:51
0

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

Maura Pintor
  • 198
  • 1
  • 14
  • Thanks for your reply, unfortunatly, i created a test.py... and the error is the same: Traceback (most recent call last): File "c:\Users\nom\Downloads\test.py", line 6, in response = requests.get(url) AttributeError: module 'requests' has no attribute 'get' – textSolver34761 Feb 19 '21 at 13:36
  • Check all imports and file names, I think it might be worth taking a look at this [older question](https://stackoverflow.com/questions/59762996/how-to-fix-attributeerror-partially-initialized-module). Hope it solves the problem, otherwise it should be a problem with circular imports as the error message suggests. – Maura Pintor Feb 19 '21 at 13:44
  • Yes, that worked... but got another error – textSolver34761 Feb 19 '21 at 13:48
  • requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /twitter/twitter_comment.csv (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] Aucune connexion n’a pu être établie car l’ordinateur cible l’a expressément refusée')) – textSolver34761 Feb 19 '21 at 13:51
  • That is probably because you need to create a web server for serving the file. One similar to [this simple web server](https://pythonbasics.org/webserver/) should be enough. The module `requests` is trying to connect to a port that is not open, as you are not running a web server locally for that. I don't think you can do it without creating a web server. – Maura Pintor Feb 19 '21 at 14:03
  • thanks. I have a 404 error, but this is what I was looking for ! Thanks. I'm going to use that and hopefully it will work! Thanks @Maura Pintor – textSolver34761 Feb 19 '21 at 14:14
0

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)