String Delimiter in Python

2024/11/18 7:50:55

I want to do split a string using "},{" as the delimiter. I have tried various things but none of them work.

string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3 "

Split it into something like this:

2,1,6,4,5,1
8,1,4,9,6,6,7,0
6,1,2,3,9
2,3,5,4,3

string.split("},{") works at the Python console but if I write a Python script in which do this operation it does not work.

Answer

You need to assign the result of string.split("},{") to a new string. For example:

string2 = string.split("},{")

I think that is the reason you think it works at the console but not in scripts. In the console it just prints out the return value, but in the script you want to make sure you use the returned value.

https://en.xdnf.cn/q/118705.html

Related Q&A

Wrong encoding of email attachment

I have a python 2.7 script running on windows. It logs in gmail, checks for new e-mails and attachments:#!/usr/bin/env python # -*- coding: utf-8 -*-file_types = ["pdf", "doc", &quo…

Blank lines in txt files in Python

I want to write sensor values to a text file with Python. All is working fine but one thing; in the text file, there are blank lines between each value. Its really annoying because I cant put the value…

Python 3 - decode spectroscopy data (Base64, IEEE754)

Im a chemist and working with spectroscopic data that was stored as a list (501 pairs of X,Y data) of Base64-encoded floating point values according to IEEE754.I tried to get an array of X, Y data to w…

Fill new column by following conditions listed in a dictionary [duplicate]

This question already has an answer here:Insert data to new column based on conditions given in dictionary(1 answer)Closed 2 years ago.I have the dictionary specifying the value the row should take if …

Pandas - Splitting dataframe into multiple excel workbooks by column value

Im new to pandas. I have a large excel file, what I’m trying to do is split the data frame after manipulation into multiple excel workbooks. There is more or less 400 vendors and I would like each To …

Create Sections in Python

I am newbie to Python. I have large file with repetitive string through the logsExample:abc def efg gjk abc def efg gjk abc def efg gjk abc def efg gjkExpected Result--------------------Section1-------…

Process CSV files in Python - Zero Imports/No Libraries

I have CSV example like this ID,TASK1,TASK2,QUIZ1,QUIZ2 11061,50,75,50,78 11062,70,80,60,50 11063,60,75,77,79 11064,52,85,50,80 11065,70,85,50,80how do i get the Max, Min and Avg on specific Column? i…

python - debugging: loop for plotting isnt showing the next plot

I need help in debugging. I just cant figure out why its not working as expected.The Code below should read data files (names are stored in all_files) in chunks of 6, arrange them in subplots (i,j indi…

Return formatted string in Python

I have a string:testString = """ My name is %s and I am %s years old and I live in %s"""I have code that finds these three strings that I want to input into testString. Ho…

How to get the greatest number in a list of numbers using multiprocessing

I have a list of random numbers and I would like to get the greatest number using multiprocessing. This is the code I used to generate the list: import random randomlist = [] for i in range(100000000):…