Splitting string in Python without breaking words [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to split a string without breaking any words in the string. After reading in a text file called "output.txt.", when I use the splitlines() function as below, it breaks the words at the line boundaries (for example, it breaks the word "from" and keeps "fr" on a first line, moving "om" to the next line.
with open('output.txt', 'r') as file:
temp = file.read()
x = ' '.join(temp.splitlines())
I am new to Python; using Python 3.6.8. Is there a way to modify the splitlines() such that it breaks the string at line boundaries and returns a list of splitted strings, but it does not break words in the original string? Thanks.

I don't know if this is the awnser you're looking for, but you can use the .split() function on a string and give a parameter to split on. So if you have:
"Hello this is a string"
and you call .split(" ") on it, it returns:
["Hello", "this", "is", "a", "string"]

Related

How to remove \ when extract words by python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to implement a function extract_words() that takes in a string of text as its single parameter and returns a list of the words in that string, like this:
input: "There're six words in this string."
output: ["There're", 'six', 'words', 'in', 'this', 'string']
But when I write it like the following and run it in Jupyter Notebook, it shows like this:
I learned how to write this function by the following, so I tried this in Jupyter, and it shows correct:
So may I ask what's wrong with my code, and how to fix it?
The problem is the cast to str inside your function. Just do return inputStr.split() and should works.
def __extract_words__(inputStr):
return inputStr.split()
inputStr = "There're six words in this string."
__extract_words__(inputStr)
There is nothing to replace. Those are escape characters because you returned a string
You want to return the split list as an actual list, not a str() - return inputStr.split()
Look at the docs for the typing module so the type checker would have shown you the issue more clearly. I.e. your function was actually returning a string.
I think what is confusing you is that you needed to use str() when you print the result, but that could be fixed like this
print(f'The list of words is {res}')
That being said, you don't really need your own function if the goal is to only print
test_string = input('text> ')
print(f'The list of words is {test_string.split()}')
Also, the __function_name__ syntax (surrounding underscores) should be reserved for "magic functions" rather than user functions

Python string rearranging [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I am wanting to rearrange strings in python, but don understand what functions would be useful in doing so? Can someone help me understand how do this: remove the first letter of a word and place that letter at the end of the word. Then you append "IE" to the end of the word. I would appreciate it, if someone can link to the functions that might be used, so I can learn how they work.
EDIT: I have tried to make this work with a phrase, but I am having issues with getting it to the ie to go at the end of the words. For example HankTIE ouYIE would be the output of the input Thank You.
Here is what I have:
string = input("Please input a word: ")
def silly_encrypter(string):
words = string.split()
for words in string:
first_letter= words[1:] + words[0]
ie_end = first_letter + "IE"
print (ie_end)
silly_encrypter(string)
As other users pointed out, I strongly suggest you to read Python tutorial, which is very friendly and contains a lot of examples that you can try out in your python console.
Having said that, you could take advantage of string indexing plus concatenation to accomplish the things you want (Both things are mentioned in the tutorial):
remove the first letter of a word and place that letter at the end of the word:
s = "myString"
first_letter_at_the_end = s[1:] + s[0]
# If you print `first_at_the_end` you'll get: 'yStringm'
then append "IE" at the end
ie_at_the_end = first_letter_at_the_end + "IE"
# If you print `ie_at_the_end` you'll get: 'yStringmIE'

Python Username Generator [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following is my code:
string1 = (input("What is your name?")) #creates a string, stores the name in it
first1 = string1[:1]
string2 = (input("What is your last name?"))
first3 = string2[:3]
from random import randint
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
It works, but I am having some trouble. I need to loop this 5 times - but it doesn't work for some reason!
P.S. Python 3!
You are creating a tuple called sentence, rather than a string
If you change that line to this:
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
It will create a string that has no gaps, like so when printed:
What is your name?First
What is your last name?Last
5fLAS
You are creating a list, not a string so it seems logical that you get issues when trying to print it...
To append to a string you can do that :
sentence = str(randint(0,9))+(first1.lower())+(first3.upper())
In Python, you don't give a type to your variables, it depends of what you put INTO them.
In Python, elements between parenthesis "()" are considered as TUPLE (a type similar to a list, but that cant be modified), "[]" stands for a list, "{}" stands for a dictionnary. So you must NOT put parts of a string between parenthesis and separated with commas or you will turn them into a list of words. Instead you can use "+" that means to Python that you are making a string concatenation. Also note that i casted your random number into string. If you give 3 strings separated by "+" Python will automatically detect sentence as a String. Wp you've created a string !
If you have a list and want to get rid of the spaces you can also use :
"".join(yourlist)
However i don't recommend it here, you would be creating a list and using a join for nothing since you can create a string from the begining.
Edit: As for the loop, we can't answer if you don't copy paste the code of the loop and the error, it could be a million things.

Find and remove duplicate words in lines in a file using python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to remove extra words that follow the first one in sequence separated by ";" on each line and return just one copy of that sequnce in a line:
Data:
XTY1;XTY3;XTY3;XTY3;XTY2;XTY1;XTY1;XTY1
XTY3;XTY4;XTY4;XTY3;XTY2;XTY7;XTY7;XTY1
XTY10;XTY3;XTY4;XTY2;XTY2;XTY11;XTY11;XTY1
Required output:
XTY1;XTY3;XTY2;XTY1
XTY3;XTY4;XTY3;XTY2;XTY7;XTY1
XTY10;XTY3;XTY4;XTY2;XTY11
My code is as follows:
for line in cluster3_urls:
list_of_words = line.split(',')
for i in list_of_words:
next_word = list_of_words[list_of_words.index(i) + 1]
if list_of_words == next_word:
list_of_words=list_of_words
print list_of_words
Can someone please let me know why my code did not work?
Many things wrong with your code. Consider itertools.groupby:
from itertools import groupby
input = 'XTYYY1;XTYYY3;XTYYY3;XTYYY3;XTYYY2;XTYYY1;XTYYY1;XTYYY1'
output = ';'.join([k for k, g in groupby(input.split(';'))])
# output: 'XTYYY1;XTYYY3;XTYYY2;XTYYY1'
I think the problem is that you are spiting based on commas instead of semicolon
try change the line to
list_of_words = line.split(';')
Your code right now is failing because you are splitting on the wrong delimiter. In addition, once you fix that, your code will additionally fail on next_word = list_of_words[list_of_words.index(i) + 1] when you reach the last word. The rest of your code just makes no sense, with your if statement comparing an array to an element of that array, then setting that arrow to itself (doing nothing). I recommend you completely rewrite your code.

How to Empty Lines? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I ran into a problem while trying to put lines from a .txt file into a list. I know you get extra lines when you do this, so I used line.split() to take out the trailing lines.
When I did this, the words I was trying to read became weirdly formatted. This is what it looked like...
['word']
Do any of you know how to take out the trailing lines without having this happen?
Just read all of lines with readlines() function and then you can get the n last line with reverse indexing : lines[-n:] and as says in comment its better to call the built-in list of open file handle !
with open('test_file.txt','r') as f :
lines =list(f)
or
with open('test_file.txt','r') as f :
lines =f.readlines()

Categories

Resources