简体   繁体   English

读取特定字符时拆分字符串

[英]Split string when a certain character is read

I have several strings stored in a file one per line like this: 我在文件中每行存储了几个字符串,如下所示:

dsfsdfsd/mhgjghj
cvcv/xcvxc
werwr/erewrwer
nbmbn/iuouiouio
...

As you can see the only character that is always present is the backlash / , the rest being pretty random in its composition. 如您所见,始终存在的唯一字符是反冲/ ,其余字符在组成上相当随机。 I need to store the first and second part (ie: before and after the backlash respectively) of each line separately, so as to end up with something like this: 我需要分别存储每行的第一部分和第二部分(即:分别在反冲之前和之后),以便最终得到这样的结果:

first_list = [dsfsdfsd, cvcv, werwr, nbmbn, ...]
secnd_list = [mhgjghj, xcvxc, erewrwer, iuouiouio, ...]

I could do this in python iterating through each line, checking for the existence of the backlash and storing the contents of each part of the line separately. 我可以通过python遍历每一行,检查是否存在反冲并分别存储该行各部分的内容来做到这一点。 It would look like this: 它看起来像这样:

first_list, secnd_list = [], []
for line in file:
    for indx, char in enumerate(line):
        if char == '/':
            first_list.append(line[:(indx-1)])
            secnd_list.append(line[(indx-1):])
            break

I'm looking for a prettier (more pythonic) version of this code. 我正在寻找此代码的更漂亮(更Pythonic)版本。

split() might come in handy here: split()在这里可能会派上用场:

first_list, secnd_list = [], []
for line in file:
    first, second = line.split('/')
    first_list.append(first)
    secnd_list.append(second)

One of the assumptions made here is that only a single / is present. 此处所做的假设之一是仅存在一个/ Knowning that, split('/') will always return a 2-tuple of elements. 知道了, split('/')将始终返回2个元素的元素。 If this assumption is false, try split('/', 1) instead - it limits the number of splits to 1, counting left-to-right. 如果此假设为假,请尝试使用split('/', 1) -将拆分的数量限制为1(从左到右)。

As well as str.split you could use str.partition : 除了str.split您还可以使用str.partition

first_parts = []
second_parts = []
for line in file:
    before, _, after = line.partition('/')
    first_parts.append(before)
    second_parts.append(after)

An alternative more functional oneliner: 另一种功能更强大的oneliner:

first_parts, _, second_parts = zip(*(line.partition('/') for line in file))

Explanation for the _ in both options - str.partition returns a tuple: (first_part, seperator, last_part) . 两个选项中_说明str.partition返回一个元组: (first_part, seperator, last_part) Here, we don't need the seperator (indeed I can't imagine why you ever would), so we assign it to the throwaway variable _ . 在这里,我们不需要分隔符(实际上我无法想象为什么会这样),因此我们将其分配给一次性变量_

Here are the docs for str.partition , and here are the docs for str.split . 下面是该文档str.partition ,并在这里是为文档str.split

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM