根据文件中的制表符拆分字符串 [英] splitting a string based on tab in the file

查看:31
本文介绍了根据文件中的制表符拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含由制表符(\t")分隔的值的文件.我正在尝试创建一个列表并将文件的所有值存储在列表中.但我遇到了一些问题.这是我的代码.

line = "abc def ghi"values = line.split("\t")

只要每个值之间只有一个选项卡,它就可以正常工作.但是如果有不止一个选项卡,那么它也会将该选项卡复制到值中.在我的情况下,额外的选项卡通常位于文件中的最后一个值之后.

解决方案

您可以在这里使用 regex:

<预><代码>>>>进口重新>>>strs = "foo\tbar\t\tspam">>>re.split(r'\t+', strs)['foo', 'bar', '垃圾邮件']

更新:

您可以使用 str.rstrip 去掉尾随的 '\t' 然后应用正则表达式.

<预><代码>>>>yas = "yas\t\tbs\tcda\t\t">>>re.split(r'\t+', yas.rstrip('\t'))['yas', 'bs', 'cda']

I have file that contains values separated by tab ("\t"). I am trying to create a list and store all values of file in the list. But I get some problem. Here is my code.

line = "abc def ghi"
values = line.split("\t")

It works fine as long as there is only one tab between each value. But if there is one than one tab then it copies the tab to values as well. In my case mostly the extra tab will be after the last value in the file.

解决方案

You can use regex here:

>>> import re
>>> strs = "foo\tbar\t\tspam"
>>> re.split(r'\t+', strs)
['foo', 'bar', 'spam']

update:

You can use str.rstrip to get rid of trailing '\t' and then apply regex.

>>> yas = "yas\t\tbs\tcda\t\t"
>>> re.split(r'\t+', yas.rstrip('\t'))
['yas', 'bs', 'cda']

这篇关于根据文件中的制表符拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆