我正在使用此字符串读取csv文件:
Allergic granulomatosis angiitis (disorder) ;http://purl.bioontology.org/ontology/SNOMEDCT/;82275008;Drug allergy ;http://purl.bioontology.org/ontology/SNOMEDCT/;414285001;Drug class allergen ;;82275008;Mild;255604002;108
我把它传递给像这样的python函数:
使用Javascript:
function readfile(f) {
var text = ""
var reader = new FileReader(); // Create a FileReader object
reader.readAsText(f); // Read the file
reader.onload = function() { // Define an event handler
text = reader.result; // This is the file contents
alert(text);
var out = document.getElementById("output"); // Find output element
out.innerHTML = ""; // Clear it
}
reader.onerror = function(e) { // If anything goes wrong
console.log("Error", e); // Just log it
};
}
function importAllergies() {
window.location="importAllergies/"+text;
}
HTML:
Import allergies:
<input type="file" onchange="readfile(this.files[0])"></input>
<pre id="output"></pre> <input id="clickMe" type="button" value="Import" onclick="importAllergies();" />
我在views.py中的函数是:
def importAllergies(request,stringP):
record_id = request.session['record_id']
INDIVO_IP = settings.INDIVO_IP
splitted = stringP.split(';')
params = {'allergic_reaction_title': splitted[0],
'allergic_reaction_system': 'http://purl.bioontology.org/ontology/SNOMEDCT/',
'allergic_reaction_identifier': splitted[2],
'category_title': splitted[3]}
urls.py:
(r'^bulkimport/importAllergies/(?P<stringP>[^/]+)', importAllergies),
除链接外,一切正常。分裂[1] ==&#34; http:&#34; 这似乎让人感到困惑&#39 ;;&#39;用&#39;:&#39; 。而且,如果我尝试使用splitted [2],则有一个例外:
Exception Type: IndexError
Exception Value:
list index out of range
如果删除链接拆分工作正常。
答案 0 :(得分:1)
对我来说,完全正常,完全您提供的示例:
>>> stringP = "Allergic granulomatosis angiitis (disorder) ;http://purl.bioontology.org/ontology/SNOMEDCT/;82275008;Drug allergy ;http://purl.bioontology.org/ontology/SNOMEDCT/;414285001;Drug class allergen ;http://purl.bioontology.org/ontology/NDFRT/;82275008;Mild;http://purl.bioontology.org/ontology/SNOMEDCT/;255604002;108"
>>> splitted = stringP.split(';')
>>> splitted
['Allergic granulomatosis angiitis (disorder) ', 'http://purl.bioontology.org/ontology/SNOMEDCT/', '82275008', 'Drug allergy ', 'http://purl.bioontology.org/ontology/SNOMEDCT/', '414285001', 'Drug class allergen ', 'http://purl.bioontology.org/ontology/NDFRT/', '82275008', 'Mild', 'http://purl.bioontology.org/ontology/SNOMEDCT/', '255604002', '108']
包含链接的所有内容,splitted[2]
没有错误,等等。因此,您不能确切地向我们展示不适合您的内容。
请编辑您的Q以在>>>
解释器提示中重复这三个步骤,复制并粘贴结果(缩进4个空格以使其可读),以确认这一点;然后,让我们看看你在代码中做了什么,与你在问题中所展示的不同。
编辑:鉴于该错误实际上在你的正则表达式
(?P<stringP>[^/]+)'
只需将后者编辑为
(?P<stringP>.+)'
为什么你想要在第一个/
完全停止匹配?!一直到URL的末尾,事情应该更好......