I need to convert string to dictionary in a manner for example Output what I require is Note: str1 can have many more such c, d, e with range. Answer You can do it with a regex: Explanation of the regex: match combination of alphanumeric characters and dashes [w-]+ followed by a space, a colon and a space _:_…
Tag: string
Add string and integer from one line string generate by speech recognizer to 2 separate list
I have some string from speech recognizer and the value is random but in the same form (x,y…n) How to add string and integer into separate list in the correct order like MyOrder? or to filter x,y (string,int or vice versa) along those strings in one line for e.g: I tried to add the list string using con…
I have written this function to get the size of a string, can it be optimized?
I don’t want to use python functions to calculate but I want to implement a function myself. The problem comes when the chain is very long. The program starts to take about 5 minutes. Answer Why don’t you use len()? I think what you’re trying to do is just len(‘holaaaaaa’) and I …
Counting word frequency in a sentence
I have two columns – one with sentences and the other with single words. Sentence word “Such a day! It’s a beautiful day out there” “beautiful” “Such a day! It’s a beautiful day out there” “day” “I am sad by the sad weather” “…
Split column to multiple columns by another column value (complicated separator)
I have dataframe like: len of column1 value may be different – from 2 to 5 words, so split with space not an option. Output should be like: That topic – How to split a dataframe string column into two columns? – didn’t help coz of separator UPD. Left “side” may have 2-5 wor…
Filter elements in a list not containing multiple substrings in Python
For a list of file names file_names, I try to use code below to filter file names not containing foo or bar: Output: But it’s not working out, it should return data.xlsx. Meanwhile, it works for containing case: Out: Does someone could help to explain what’s error in my code and how to fix it? Tha…
How to extract multiple specific string lines from another string?
I’m using a FEM software (Code_Aster) which is developed in Python, but its file extension is .comm. However, Python snippets can be inserted in .comm files. I have a function which returns a very long string containing nodes, elements, elements group, and so on in the below form: My goal is to add each…
Get rid of single quotes from the substituted string within sed command in Python
I have a text file 1.txt which has following content: I am trying to generate multi-line string that I need to add before module abc line in 1.txt. I am using sed to do this. This is the sed command I am forming and printing before executing it: When I execute the above sed command, I get error: There shouldn…
Split a string from a textfile correctly
i have a file (“text.txt”) with several lines (~5000000 lines). I’m trying to split a line so that: becomes this two lines: so essentially i want to transform a single line into two lines and write it back to another file. All the lines that has to be split starts with the character “&…
why do string.strip() function removed the character i want in some words and remove none in others
i am trying to remove the ‘n’ characters a string because when i read the lines from a file it include the ‘n’ characters!! input: output: Answer Because you are modifying the list while iterating over it, halfway through the loop, list_of_names.remove(name) is trying to remove the wro…