Split String into groups [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I've a string like this Delete File/Folder. I need to break the sentence based on the / equivalent to or.
Finally need to generate two strings out of this like Delete File as one string and Delete Folder as the other one.
I've tried very naive way where I check for the index of / and then form strings with a bunch of conditions.
It some times fails when we have string like File/Folder Deleted.
Edit:
If you split on / then for case 1 we have Delete File and Folder. Then I'll check for spaces present in first string and spaces present is second string.
The one which has less number of spaces will be replaced with first string last element. This is getting complicated.

In the case of Delete File/Folder, thinking through why the word Delete gets distributed to both of File and Folder might help with the inherent assumptions we all intuitively make when lexical parsing.
For instance, it would be parsed between the the i and l to return ["Delete File", "Delete FiFolder"].
It sounds like you want to want to split the string into words based on where there are spaces and then split each word based on / to generate new full strings.
>>> import itertools
>>> my_str = "Delete File/Folder"
>>> my_str = ' '.join(my_str.split()).replace('/ ', '/').replace(' /', '/') # First clean string to ensure there aren't spaces around `/`
>>> word_groups = [word.split('/') for word in my_str.split(' ')]
>>> print [' '.join(words) for words in itertools.product(*word_groups)]
['Delete File', 'Delete Folder']

Do you want that? Comment if you want a more generalized solution.
lst = your_string.split()[1].split("/")
finalList=[]
for i in lst:
finalList.append("Delete {0}",i)
print finalList
For string:
Delete File/Folder
Output:
['Delete File', 'Delete Folder']

str = "Do you want to Delete File/Folder?"
word = str.split(" ")
count = str.count("/")
c = True
for j in range(0,2*count):
for i in word:
if("/" in i):
words = i.split("/")
if c:
print words[1],
else:
print words[0],
else:
print i, # comma not to separate line
c = not c
print
output
Do you want to Delete File
Do you want to Delete Folder?

st1 = "Do you want to Delete File/Folder"
st2 = "File/Folder Updated"
def spl(st):
import re
li = []
ff = re.search(r'\w+/\w+',st).group()
if ff:
t = ff.split('/')
l = re.split(ff,st)
for el in t:
if not l[0]:
li.append((el + ''.join(l)))
else:
li.append((''.join(l) + el))
return li
for item in st1,st2:
print(spl(item))
['Do you want to Delete File', 'Do you want to Delete Folder']
['File Updated', 'Folder Updated']

Related

Question: Python String.split function problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a problem like:
name = "Steve Jobs is my name."
Desired output:
['Steve Jobs', 'is', 'my name.']
My tried solution:
I have tried using the split function in Python.
Code:
result = name.split()
['Steve', 'Jobs', 'is', 'my', 'name']
I am unable to proceed from here. I have tried using for loop.
But, I am not getting the required result.
Am I missing something or is there any other way of solving this problem?
Please let me know.
This seems like the kind of interview question where they just want to find out how you think. In this case, with just one example to go on, I would look for a pattern that gets you to the desired output.
For example, you could split on " is " to get the two parts before and after, then insert "is" back in the middle of the list to get the desired result.
name = "Steve Jobs is my name."
result = name.split(" is ")
result .insert(1, "is")
print(result )
Assuming this is a trick question:
[x.strip() for x in name.partition("is")]
If you split the string using string.split(), the string will be split at ever occurrence of " " (space). But in your case, you want to split it at a specific place instead of at every occurrence of space.
you can try using:
result1 = [name[:10], name[11:13], name[14:]]
but this is only applicable if you wanna split this specific string.
If you want to split different strings that are of similar sentence structure, you can try:
words = name.split()
result = [words[0] + " " + words[1], words[2], words[3] + " " + words[4]]

Python and Regex - Replace Date [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have multiple strings in the form of :
AM-2019-04-22 06-47-57865BCBFB-9414907A-4450BB24
And I need the month from the date part replaced with something else, for example:
AM-2019-07-22 06-47-57865BCBFB-9414907A-4450BB24
How can I achieve this using python and regex?
Also, I have multiple text files that contain a line similar to this:
LocalTime: 21/4/2019 21:48:41
And I need to do the same thing as above (replace the month with something else).
For your first example:
import re
string = 'AM-2019-07-22 06-47-57865BCBFB-9414907A-4450BB24'
replace = '0'+str(int((re.match('(AM-\d{4}-)(\d{2})',string).group(2)))+2)
re.sub('(AM-\d{4}-)(\d{2})',r'\g<1>'+replace,string) #Replace 07 for 07 +2
Output:
Out[10]: 'AM-2019-09-22 06-47-57865BCBFB-9414907A-4450BB24'
For the second one:
string2 = 'LocalTime: 21/4/2019 21:48:41'
replace = str(int((re.match(r'(LocalTime: \d{1,2}/)(\d{1,2}).*',string2).group(2)))+2)
re.sub('(Time: \d{2}/)(\d{1,2})',r'\g<1>'+replace,string2) #Replace 4 for 6
Output:
Out[14]: 'LocalTime: 21/6/2019 21:48:41'
If you want to limit the months in which this operation is done, you can use an if statement:
if re.match('(AM-\d{4}-)(\d{2})',string).group(2).isin(['04','05','06']:
if re.match(r'(LocalTime: \d{1,2}/)(\d{1,2}).*',string2).group(2).isin(['4','5','6']:
Similar answer but with more code and a lookbehind.
First question:
import re
#This could be any number of strings, or some other iterable
string_list = []
string_list.append("AM-2019-04-22 06-47-57865BCBFB-9414907A-4450BB24")
string_list.append("AM-2019-07-22 06-47-57865BCBFB-9414907A-4450BB24")
#This checks for four digits and a hyphen, then any number of digits to
#replace (which is the month
pattern = r"(?<=\d{4}-)\d+"
#This should be a string
month = "08"
for string in string_list:
print("BEFORE: " + string)
string = re.sub(pattern, month, string)
print("AFTER: " + string)
Second question:
import re
#checks for a colon, 2 numbers, a forward slash, and then selects however many numbers after (which is the month)
pattern = r"/(?<=: \d{2}/)\d+"
#IMO it's better just to write to another file. You can edit the current file you're on, but it's cleaner this way and you won't accidentally screw it up if my regex is wrong.
in_file = open("mytextfile.txt", 'r')
out_file = open("myoutputfile.txt", 'w')
#This should be a string
month = "9"
for line in in_file:
changed_line = re.sub(pattern, month, line)
out_file.write(changed_line)
in_file.close()
out_file.close()
Hope this helps.

Python code that search for text and copy it to the next line [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I python code to read lines in a text file and to copy text between specific characters. For example, text between _ _.
Input
./2425/1/115_Lube_45484.jpg 45484
./2425/1/114_Spencerian_73323.jpg 73323
Output
./2425/1/115_Lube_45484.jpg 45484
Lube
./2425/1/114_Spencerian_73323.jpg 73323
Spencerian
Any suggestions?
Instead of regex i would use build in: split()
input = './2425/1/114_Spencerian_73323.jpg 73323'
output = input.split('_')[1]
print(output)
Of course if every line has double _ in input string
Try this:
import re
for line in your_text.splitlines():
result = re.match("_(.*)_", your_text)
print(match.group(0))
print(match.group(1))
Where your_text is a string containing your example as above.
test = './2425/1/114_Spencerian_73323.jpg_abc_ 73323'
result = test.split("_",1)[1].split("_")[0]
print(result)
.split('',1) splits the string in 2 parts i-e: 0 index will be left substring of '' and 1 index will be right substring of string. We again split the right part of string with '_' so that the text between _ will be extracted.
Note : this will be helpful only when there is single occurence of text between _ like test. It wont extract text if there exist this case multiple times in a string
Solved.
file_path = "text_file.txt"
with open(file_path) as f:
line = f.readline()
count= 1
while line:
print(line,line.split('_')[1])
line = f.readline()
count+= 1
Thank you all

Special characters to the end of sentence [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
For a random string such as:
H!i I am f.rom G3?ermany
how can I move all the special characters to the end of the word, for instance:
Hi! I am from. Germany3?
You can try this one :
s = "H!i I am f.rom G3?ermany"
l = []
for i in s.split():
k = [j for j in i if j.isalpha()]
for m in i:
if not m.isalpha():
k.append(m)
l.append(''.join(k))
print(' '.join(l))
It will o/p like :
"Hi! I am from. Germany3?
In python 2x you can do it in single line like :
k = ' '.join([filter(str.isalpha,i)+''.join([j for j in i if not j.isalpha()]) for i in s.split()])
I'm defining special character as anything thats not a-z, A-Z or spaces:
You can split the string into words, use regex to find the special characters in each word, remove them, add them back to the end of the word, then join the words together to create your new string:
import re
string = "H!i I am f.rom G3?ermany"
words = string.split(' ')
pattern = re.compile('[^a-zA-Z\s]')
new = ' '.join([re.sub(pattern, '', w) + ''.join(pattern.findall(w)) for w in words])
That will turn H!i I am f.rom G3?ermany into Hi! I am from. Germany3?

Python script search a text file for a word [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm writing a Python script. I need to search a text file for a word that end by " s , es or ies " and the word must be greater than three letters , need to konw number of words and the word it-self .....it's hard task i cant work with it, please help me
I agree with the comment that you need to go work on the basics. Here are some ideas to get you started.
1) You say "search a file." Open a file and read line by line like this:
with open ('myFile.txt', 'r') as infile:
for line in infile:
# do something to each line
2) You probably want to store each line in a data structure, like a list:
# before you open the file...
lines = []
# while handling the file:
lines.append(line)
3) You'll need to work with each word. look into the 'split' function of lists.
4) You'll need to look at individual letters of each word. Look into 'string slicing.'
All said and done, you can probably do this with 10 - 15 lines of code.
Try to divide the task into different tasks if it feels overwhelming.
The following code is by no means good, but hopefully it is clear enough so you can get the point.
1 First you need to get your text. If your text is in a file in your computer you need to put it into something that python can use.
# this code takes the content of "text.txt" and store it into my_text
with open("text.txt") as file:
my_text = file.read()
2 Now you need to work with every individual word. All your words are together in a string called my_text, and you would like them separated (split) into a list so you can work with them individually. Usually words are separated by spaces, so that's what you use to separate them:
# take the text and split it into words
my_words = my_text.split(" ")
3 I don't know exactly what you want, but let's suppose you want to store separately the words in different lists. Then you will need those lists:
# three list to store the words:
words_s = []
words_es = []
words_ies = []
4 Now you need to iterate through the words and do stuff with them. For that the easiest thing to do is to use a for loop:
#iterate through each word
for word in my_words:
# you're not interested in short words:
if len(word) <= 3:
continue # this means: do nothing with this word
# now, if the word's length is greater than 3, you classify it:
if word.endswith("ies"):
words_ies.append(word) # add it to the list
if word.endswith("es"):
words_es.append(word) # add it to the list
if word.endswith("s"):
words_s.append(word) # add it to the list
4 Finally, outside the for loop, you can print the list of words and also get the length of the list:
print(words_s)
print(len(words_s))
Something that you need to consider is if you want the words repeated or not. Note that the condition 'word that end by "s", "es" or "ies"' is equivalent to 'word that end by "s"'. The code above will get the words distributed in different lists redundantly. If a word ends with "ies" it also ends with "es" and "s", so it'll be stored in the three lists. If you want to avoid overlapping, you can substitute the if statements by else if statements.
Keep learning the basics as other answers suggest and soon you'll be able to understand scary code like this :D
with open("text.txt") as myfile:
words = [word for word in myfile.read().split(" ") if word.endswith("s") and len(word) > 3]
print("There are {} words ending with 's' and longer than 3".format(len(words)))

Categories

Resources