Используя MultipartPostHandler к данным формы POST с Python

Попробуйте использовать pd.melt() , проще:

df.melt('Name',var_name='Dates',value_name='Number')

    Name    Dates  Number
0  andre  22-2019       5
1   Marc  22-2019      12
2  andre  23-2019       3
3   Marc  23-2019      64

Или:

m=df.set_index('Name').stack().reset_index()
m.columns=['Name','Date','Number']
print(m)

    Name     Date  Number
0  andre  22-2019       5
1  andre  23-2019       3
2   Marc  22-2019      12
3   Marc  23-2019      64

47
задан abdullahkhawer 17 September 2019 в 17:52
поделиться

2 ответа

Кажется, что самый легкий и самый совместимый способ обойти эту проблему состоит в том, чтобы использовать модуль 'плаката'.

# test_client.py
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

# Register the streaming http handlers with urllib2
register_openers()

# Start the multipart/form-data encoding of the file "DSC0001.jpg"
# "image1" is the name of the parameter, which is normally set
# via the "name" parameter of the HTML <input> tag.

# headers contains the necessary Content-Type and Content-Length
# datagen is a generator object that yields the encoded parameters
datagen, headers = multipart_encode({"image1": open("DSC0001.jpg")})

# Create the Request object
request = urllib2.Request("http://localhost:5000/upload_image", datagen, headers)
# Actually do the request, and get the response
print urllib2.urlopen(request).read()

Это работало прекрасное, и я не должен был унавоживать с httplib. Модуль доступен здесь: http://atlee.ca/software/poster/index.html

57
ответ дан Dan 26 November 2019 в 19:21
поделиться

Найденный этим рецептом для регистрации многослойного использования httplib непосредственно (никакие внешние библиотеки не включили),

import httplib
import mimetypes

def post_multipart(host, selector, fields, files):
    content_type, body = encode_multipart_formdata(fields, files)
    h = httplib.HTTP(host)
    h.putrequest('POST', selector)
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body)))
    h.endheaders()
    h.send(body)
    errcode, errmsg, headers = h.getreply()
    return h.file.read()

def encode_multipart_formdata(fields, files):
    LIMIT = '----------lImIt_of_THE_fIle_eW_$'
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + LIMIT)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for (key, filename, value) in files:
        L.append('--' + LIMIT)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        L.append('Content-Type: %s' % get_content_type(filename))
        L.append('')
        L.append(value)
    L.append('--' + LIMIT + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % LIMIT
    return content_type, body

def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
38
ответ дан Community 26 November 2019 в 19:21
поделиться
Другие вопросы по тегам:

Похожие вопросы: