Ad
How To Split String By Space And Words List
Assume that I have below string:
"USD Notional Amount: USD 50,000,000.00"
"USD Fixed Rate Payer Currency Amount: USD 10,000,000"
"USD Fixed Rate Payer Payment Dates: Annually"
"KRW Fixed Rate Payer Payment Dates: Annually"
Simply, using split function
df = pd.DataFrame(["USD Notional Amount: USD 50,000,000.00"
,"USD Fixed Rate Payer Currency Amount: USD 10,000,000"
,"USD Fixed Rate Payer Payment Dates: Annually"
,"KRW Fixed Rate Payer Payment Dates: Annually"])
df[0].apply(lambda x: x.split())
[OUTPUT]
0 [USD, Notional, Amount:, USD, 50,000,000.00]
1 [USD, Fixed, Rate, Payer, Currency, Amount:, USD, 10,000,000]
2 [USD, Fixed, Rate, Payer, Payment, Dates:, Annually]
3 [KRW, Fixed, Rate, Payer, Payment, Dates:, Annually]
I want to have preserving compound words list
words_list = ["Notional Amount:","Fixed Rate Payer Currency Amount:","Fixed Rate Payer Payment Dates:"]
What I want is to split the string into string array, like below:
["USD","Notional Amount:","USD", "50,000,000.00"]
["USD","Fixed Rate Payer Currency Amount:","USD","10,000,000"]
["USD","Fixed Rate Payer Payment Dates:","Annually"]
["KRW","Fixed Rate Payer Payment Dates:","Annually"]
When I split this string I would like to preserve some words as it is not always splitting by space. Anyone knows how to do this kind of string split in Python? Any thoughts?
Ad
Answer
As Xhattam said, there is probably no generic way to do your thing.
However, assuming that you know which strings with spaces you don't want to split, you can do the following (from your example):
test = "USD Notional Amount: USD 50,000,000.00"
a = ['Notional Amount:', 'Fixed Rate Payer Currency Amount:', 'Fixed Rate Payer Payment Dates:', 'Fixed Rate Payer Payment Dates:']
for element in a:
if element in test:
# Do this to strip your string from the list
my_list = test.replace(element, '')
# Do this to replace double space by simple space following the word stripping
my_list = test.replace(' ', ' ')
# Insert the element you striped in the list at the wanted index
my_list.insert(1, element)
break
Now you should be able to print my_list and get the following result:
print(my_list)
['USD', 'Notional Amount:', 'USD', '50,000,000.00']
This is a specific example you can easily adapt to your other strings.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad