简体   繁体   English

如何在特定点拆分字符串?

[英]How do you split a string at a specific point?

I am new to python and want to split what I have read in from a text file into two specific parts. 我是python的新手,想把我读过的文本文件拆分成两个特定的部分。 Below is an example of what could be read in: 以下是可以阅读的示例:

f = ['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]

So what I want to achieve is to be able to execute the second part of the program is: 所以我想要实现的是能够执行程序的第二部分是:

words = ['Cats','like','dogs','as','much','cats.']

numbers = [1,2,3,4,5,4,3,2,6]

I have tried using: 我尝试过使用:

words,numbers = f.split("][")

However, this removes the double bracets from the two new variable which means the second part of my program which recreates the original text does not work. 但是,这会从两个新变量中删除双重支撑,这意味着我的程序的第二部分重新创建原始文本不起作用。

Thanks. 谢谢。

I assume f is a string like 我假设f是一个字符串

f = "['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]" 

then we can find the index of ][ and add one to find the point between the brackets 然后我们可以找到][的索引][并添加一个来查找括号之间的点

i = f.index('][')
a, b = f[:i+1], f[i+1:]
print(a)
print(b)

output: 输出:

['Cats','like','dogs','as','much','cats.']
[1,2,3,4,5,4,3,2,6]

Another Alternative if you want to still use split() 如果你想仍然使用split()另一种选择

f = "['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]"
d="]["
print f.split(d)[0]+d[0]
print d[1]+f.split(d)[1]

If you can make your file look something like this: 如果您可以使您的文件看起来像这样:

[["Cats","like","dogs","as","much","cats."],[1,2,3,4,5,4,3,2,6]]

then you could simply use Python's json module to do this for you. 那么你可以简单地使用Python的json模块为你做这件事。 Note that the JSON format requires double quotes rather than single. 请注意,JSON格式需要双引号而不是单引号。

import json
f = '[["Cats","like","dogs","as","much","cats."],[1,2,3,4,5,4,3,2,6]]'
a, b = json.loads(f)
print(a)
print(b)

Documentation for the json library can be found here: https://docs.python.org/3/library/json.html 有关json库的文档,请访问: https//docs.python.org/3/library/json.html

An alternative to Patrick's answer using regular expressions: 使用正则表达式替代Patrick的答案:

import re

data = "f = ['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]"
pattern = 'f = (?P<words>\[.*?\])(?P<numbers>\[.*?\])'

match = re.match(pattern, data)
words = match.group('words')
numbers = match.group('numbers')

print(words)
print(numbers)

Output 产量

['Cats','like','dogs','as','much','cats.']
[1,2,3,4,5,4,3,2,6]

If I understand correctly, you have a text file that contains ['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6] and you just need to split that string at the transition between brackets. 如果我理解正确,你有一个文本文件,其中包含['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6] ,你只需要在括号之间的过渡处拆分该字符串。 You can do this with the string.index() method and string slicing. 您可以使用string.index()方法和字符串切片来完成此操作。 See my console output below: 请参阅下面的控制台输出:

>>> f = open('./catsdogs12.txt', 'r')
>>> input = f.read()[:-1]  # Read file without trailing newline (\n)
>>> input
"['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]"
>>> bracket_index = input.index('][')  # Get index of transition between brackets
>>> bracket_index
41
>>> words = input[:bracket_index + 1]  # Slice from beginning of string
>>> words
"['Cats','like','dogs','as','much','cats.']"
>>> numbers = input[bracket_index + 1:]  # Slice from middle of string
>>> numbers
'[1,2,3,4,5,4,3,2,6]'

Note that this will leave you with a python string that looks visually identical to a list (array). 请注意,这将为您提供一个与列表(数组)在视觉上完全相同的python字符串。 If you needed the data represented as python native objects (ie so that you can actually use it like a list), you'll need to use some combination of string[1:-1].split(',') on both strings and list.map() on the numbers list to convert the numbers from strings to numbers. 如果你需要表示为python本机对象的数据(即你可以像列表一样使用它),你需要在两个字符串上使用string[1:-1].split(',')某种组合和数字列表上的list.map()将数字从字符串转换为数字。

Hope this helps! 希望这可以帮助!

Another thing you can do is first replace ][ with ]-[ and then do a split or partition using - but i will suggest you to do split as we don't want that delimiter. 你可以做的另一件事是首先替换][ with ]-[然后使用-进行拆分或分区-但我会建议你拆分,因为我们不想要这个分隔符。

SPLIT 分裂

f = "['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]" 
f = f.replace('][',']-[')
a,b = f.split('-')

Output 产量

>>> print(a)
['Cats','like','dogs','as','much','cats.']
>>> print(b)
[1,2,3,4,5,4,3,2,6]

PARTITION 划分

f = "['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]"
f = f.replace('][',']-[')
a,b,c = f.partition('-')

Output 产量

>>> print(a)
['Cats','like','dogs','as','much','cats.']
>>> print(c)
[1,2,3,4,5,4,3,2,6]

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

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