我正在尝试将复制/粘贴的文本转换为csv,我可以将其拆分。 问题是它里面有空白标签,我似乎无法摆脱
复制/粘贴示例:
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']
答案 0 :(得分:8)
我发现有时会有几个\t
。我使用re
模块正确分割:
for line in lines:
linedata = re.split(r'\t+', line)
print ",".join(linedata)
答案 1 :(得分:2)
您可以在标签上split
:
line = line.split('\t')
除非您特别需要以逗号分隔的值,否则您只需将文字直接粘贴到选项卡上的open
,split
文件中并使用数据即可没有引入逗号。