我想將複製/粘貼的文本轉換爲csv,我可以拆分後。 問題是有它的空白標籤,我似乎無法擺脫python拆分字符串上的空白
例的複製/粘貼:
Amarr Hybrid Tech Decryptor 12 Decryptors - Hybrid 12 m3
Ancient Coordinates Database 23 Sleeper Components 2.30 m3
Caldari Hybrid Tech Decryptor 17 Decryptors - Hybrid 17 m3
Carbon 17 General 34 m3
Cartesian Temporal Coordinator 4 Ancient Salvage 0.04 m3
Central System Controller 2 Ancient Salvage 0.02 m3
現在我想要得到的東西是這樣的:
Amarr Hybrid Tech Decryptor,12,Decryptors - Hybrid,12,m3,
Ancient Coordinates Database,23,Sleeper Components,2.30,m3,
Caldari Hybrid Tech Decryptor,17,Decryptors - Hybrid,17,m3,
Carbon,17,General,34,m3,
Cartesian Temporal Coordinator,4,Ancient Salvage,0.04,m3,
Central System Controller,2,Ancient Salvage,0.02,m3,
(永遠是每行的5個分離
我一直在努力做這在各種方式 Split by comma and strip whitespace in Python 但我似乎無法得到它的工作。
@login_required
def index(request):
if request.method == "POST":
form = SellListForm(request.POST)
if form.is_valid():
selllist = form.save(commit=False)
selllist.user = request.user
string = selllist.sell
string = [x.strip() for x in string.split(',')]
print string
return HttpResponseRedirect(reverse('processed'))
else:
form = SellListForm()
return render(request, 'index.html', {'form': form})
回報
[u'<<<SULTS STUFF>>>\t\t\tVoucher\t\t\t0 m3\r\nAmarr Hybrid Tech Decryptor\t12\tDecryptors - Hybrid\t\t\t12 m3\r\nAncient Coordinates Database\t23\tSleeper Components\t\t\t2.30 m3\r\nCaldari Hybrid Tech Decryptor\t17\tDecryptors - Hybrid\t\t\t17 m3\r\nCarbon\t17\tGeneral\t\t\t34 m3\r\nCartesian Temporal Coordinator\t4\tAncient Salvage\t\t\t0.04 m3\r\nCentral System Controller\t2\tAncient Salvage\t\t\t0.02 m3']
請在這裏發佈代碼。 – aIKid
可能重複[如何在Python中分割一個字符串與未知數量的空格作爲分隔符?](http://stackoverflow.com/questions/4309684/how-in-python-to-split-a-string-with-未知數的空格作爲分隔符) – tripleee
答案是使用不帶參數的分割,因爲此網站上的多個早期問題顯示爲 – Mawg