環境
OS:windows10
Redmine:ver 3.4
Python:2.7
読込対象:CSV(shift-jis)
csv
"project","title","subject"
"test","テスト","テスト"
sample.py
# -*- coding: utf-8 -*-
import csv
import urllib2
API_KEY = 'XXXXXXXXXXXXXXXXXXXXX'
URL = 'http://host_name/redmine//issues.xml'
def requests(xml):
request = urllib2.Request(URL, data=xml)
request.add_header('Content-Type', 'text/xml')
request.add_header('X-Redmine-API-Key', API_KEY)
request.get_method = lambda: 'POST'
# 登録実行
response = urllib2.urlopen(request)
ret = response.read()
print 'Response:', ret.decode('utf-8')
def main():
csv_filename = r'.\test.csv'
with open(csv_filename) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
xml = """<?xml version="1.0"?>
<issue>
<project_id>""" + row['project'] + """</project_id>
<subject>""" + row['title'] + """</subject>
<description>""" + row['subject'] + """</description>
</issue>"""
xml = xml.decode('shift-jis').encode('utf-8')
requests(xml)
if __name__ == '__main__':
main()
Python2でのRedmine APIの使い方でハマったのでメモ