0

Again, relatively new to python, so this may seem like a no-brainer to some people. My apologies in advance.

I would like to know how to open a .csv file and send the contents as data in a post session.

Something kind of like this:

userData = json.loads(loginResponse.text)
sessionToken = userData["sessionId"]
print ('Login successful! Attempting to upload file...')

# Now try to upload file
uploadURL = 'url'
headers = {
    'token': sessionToken
}


with open('data.csv', newline='') as csvFile: 
    csvReader = csv.reader(csvFile)

    uploadResponse = loginAS.post(uploadURL, headers=headers, data='CONTENTS OF CSV FILE')
    print (uploadResponse.status_code)
csvfile.close()

I have tried to just open the csv file, but that didn't work. And I've tried

data=list(csvReader)

But I get a 'too many values to unpack' error. So, any ideas?

I'm not sure if it matters, but I am using Python 3.4

Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
Apolymoxic
  • 730
  • 14
  • 34
  • If you want to pass the file contents as simply a big blob of text, don't use the csv module at all. Just use the normal file `read()` method. – John Gordon Aug 23 '16 at 14:44
  • just want to send the raw data? Or in a specific format? For your error have a look at [this](https://stackoverflow.com/questions/1479776/too-many-values-to-unpack-exception) – TryToSolveItSimple Aug 23 '16 at 14:45
  • @JohnGordan, would you please elaborate a bit more? How do I use the read() method? – Apolymoxic Aug 23 '16 at 14:46
  • `uploadResponse = loginAS.post(..., data=csvFile.read())` – John Gordon Aug 23 '16 at 14:47
  • @TryToSolveItSimple Honestly, I don't know if I need it as a specific format or not. I am trying to upload a file to a server, but when I used the files=files option, I get a 500 error. Intercepting the HTTP call, it looks like it should just a be a big blob of text.... Trying to solve the unpack values (I've read the post you suggested) seems to be moot. So I am just trying to do anything I can. I'll start with plain text – Apolymoxic Aug 23 '16 at 14:49
  • @JohnGordon. That works. Please submit as answer. =) – Apolymoxic Aug 23 '16 at 14:50

1 Answers1

1

If you want to pass the file contents as simply a big blob of text, don't use the csv module at all. Just use the normal file read() method:

uploadResponse = loginAS.post(..., data=csvFile.read())
John Gordon
  • 29,573
  • 7
  • 33
  • 58