Go from '\\n' to '\n' - python

I want to get an input from the console that is a text with several lines. Since I want to do it all in a single input I have to mark '\n' in the middle to signal the different lines.
After I get the input I want to save the text in a matrix where each line is a line from the text with the words separated.
This is my function to do so:
def saveText(text):
text = text[6:len(text)-6]
line = 0
array = [[]]
cur = ""
for i in range (len(text)):
if (text[i] == '\n'):
line+=1
array.append([])
else:
if ((text[i] == ' ' or text[i] == ',' or text[i] == ';' or text[i] == '.') and cur != ""):
array[line].append(cur)
cur = ""
else:
cur += text[i]
return array
However, when I print the variable array it appears as a matrix with only one line, and besides the '\n' are counted as words, they also appear as '\n'.
Can anyone help me with this?

You didn't provide an input string to test with, so I just made my own. You can use .split() to split on new lines and spaces (or anything else that you want).
Edit: I think I understand what you mean now. I think you are trying to have the user input newline characters \n when asking them for input. This isn't possible using input, but there is a workaround. I integrated the answer from that link into the code below.
If you instead wanted the user to manually write \n when getting input from them, then you'd need to change text.splitlines() to text.split('\\n'). You could also replace\nwith\nby usingtext.replace('\n', '\n')`.
But, I think it'd be less error prone to just use the multi-line input as shown below and discussed further in like above.
lines = []
while True:
line = input()
if line:
lines.append(line)
else:
break
input_text = '\n'.join(lines)
def save_text(text):
lines = text.splitlines()
matrix = []
for line in lines:
matrix.append(line.split(' '))
return matrix
print(save_text(input_text))
Input from user looks like this:
hello how
are you
doing this fine
day?
outputs:
[['hello', 'how'], ['are', 'you'], ['doing', 'this', 'fine'], ['day?']]

text = "line1: hello wolrd\nline2: test\nline2: i don't know what to write"
lines = [x.split() for x in text.split("\n")]
print(lines)
This will return:
[['line1:', 'hello', 'wolrd'], ['line2:', 'test'], ['line2:', 'i', "don't", 'know', 'what', 'to', 'write']]
the idea is the same as Stuart's solution, it's just a little bit more efficient.

Related

Appending a char to an empty list

I am very new to programming, so sorry for a basic question. I am trying to write a function that will take a string in which words are divided by ',' and return a list of these words (the Split method). My code is:
def str_to_list(my_shop_str):
my_shop_list = ['']
word_in_list = 0
for letter in my_shop_str:
if letter != ',':
my_shop_list[word_in_list] += letter
else:
word_in_list += 1
my_shop_list + ['']
return my_shop_list
print(str_to_list("Milk,Cottage,Tomatoes")) should look like [Milk, Cottage, Tomatoes]
but I am keep getting IndexError: list index out of range.
I read some answers here and couldn't find something to work.
Can anyone explain what is wrong.
list has the method append so a solution will be something like this:
def str_to_list(my_shop_str):
my_shop_list = ['']
word_in_list = 0
for letter in my_shop_str:
if letter != ',':
my_shop_list[word_in_list] += letter
else:
word_in_list += 1
my_shop_list.append('')
return my_shop_list
PS: Do not forgot about empty spaces between words in string like "aaa, bbb, ccc" will be ['aaa', ' bbb', ' ccc'] with spaces.
def sp(s):
l =[]
while True:
comma = s.find(',')
if comma == -1:
l.append(s)
break
l.append(s[:comma])
s = s[comma+1:]
print(l)
this is a simplified version hope it helps.
Simplest Way:
We can use an inbuilt split function.
def Convert(string):
# split the string whenever "," occurs
# store the splitted parts in a list
li = list(string.split(","))
return li
# Driver code
str1 = "Hello,World"
print(Convert(str1))
Output:
["Hello", "World"]

unusable array when passing a txt file into array

So i need to pass all elements(the all are strings) from txt file into an array to use further. I have this kind of output:
['mzm\n', 'vur\n', 'bmc\n', 'irl\n'],
but i have:
KeyError: '\n' because of this '/n's.
Is it possible to pass all strings into array to have this output [mzm, vur, bmc, irl]?
This is for my radix sort algorithm.
def main():
with open('Array.txt') as my_file:
words = my_file.readlines()
max_size = check_max_word_size(words)
new_list = set_same_size(words, max_size)
new_list = radix_sort(new_list, max_size-1, 0)
#Remove the dots previously added to the words
index = 0
for word in new_list:
new_list[index]= re.sub('[.]', '', word)
index+=1
#Print the final ordered list, all lower case
print(new_list)
if __name__ == '__main__':
main()
[mzm, vur, bmc, irl]
You can strip off the trailing newlines in word like this:
new_list[index]= re.sub('[.]', '', word.rstrip())
The characters '\n' come from the file, as readlines() keeps them.
You can remove the characters '\n' like this:
words = [w.strip('\n') for w in words]

anagram solver not working

So, this has been my project for a long time, and eventually, I have made an anagram solver in python 3.4, except it's supposed to find anagram for the word + a random letter. I have worked out all the error messages, but there are no more errors, it just doesn't do it. All help appreciated. I have had a lot of helpful comments, but it is still not working. I have updated the code in the question.(Here is the file I used with all the words of the dictionary on different lines, it's really helpful and I had to look for something like this for months.)
file = open("C:\\my stuff\\brit-a-z.txt","r")
def anagram(word):
for alpha in range(ord('a'), ord('z') + 1):
newletter = chr(alpha)
for line in file:
ref = line.strip()
word1 = list(word)
list.append(newletter)
word1_list.sort()
ref_list = list(line)
ref_list.sort()
if word1_list == ref_list:
print(line)
while True:
inp = input()
anagram(inp)
.
This should do what you need.
with open("C:\\my_folders_are_in_here\\brit-a-z.txt", 'r') as f:
check_list = [x.strip() for x in f.readlines()]
def anagram(word):
for alpha in range(ord('a'), ord('z') + 1):
newletter = chr(alpha)
for line in check_list:
word1_list = list(word + newletter)
word1_list.sort()
ref_list = list(line)
ref_list.sort()
if word1_list == ref_list:
print(line)
while True:
inp = input()
anagram(inp)
I took advantage of the chr() and ord() built-in function to remove the long if that converts alpha into newletter.
Reading lines from file in Python also includes newline characters.
So if one line in the file is the word "the" for example, assigning ref = line, ref will equal "the\n"(or "the\r\n"). Your sorted ref_list then becomes ['\n', 'e', 'h', 't']
Reading from keyboard using input(), however, does not include newline characters. Your word1_list never contains a '\n', thus, word1_list and ref_list will never be equal.
Fix: change ref = line into ref = line.strip() to remove newline characters.

How do I remove space at the end of an output in python?

I have a program that counts and prints all words in a sentence that contains a specific character(ignoring case).
Code in Python -
item=input()
ip=input().tolower()
r=ip.count(item)
print(r)
ip=ip.split()
for word in ip:
if item in word:
print((word), end=' ')
This program works as expected but for the last word that is printed I don't want a white-space after it.
If anyone could guide me on how to remove the space it would be appreciated.
Why don't you use list comprehension and str.join?
print(' '.join([w for w in ip if item in w]))
I don't think there's a way to remove that, as it's a part of your terminal. Best answer I can give you.
I expanded on the code though, cause I was kinda bored.
sentence = input("Enter a sentence: ").lower()
pull = input("Which character(s) do you want to count?: ").lower()
for c in pull:
occurrences = 0
for character in sentence:
if c == character:
occurrences+=1
if c!=" ": print("\'%s\' appears %d times"%(c, occurrences))
for word in sentence.split():
occurrences = 0
for character in word:
if c == character:
occurrences+=1
if occurrences == 1:
print(("1 time in \'%s\'")%(word))
elif occurrences > 0:
print(("%d times in \'%s\'")%(occurrences,word))
+The solution with a list comprehension appears more concise, but if you prefer an alternative you can use the following. It was tested and worked with the example in the picture.
# Amended solution. The commented lines are the amendment.
item = input('Letter: ')
ip = input('Input: ').lower()
r = ip.count(item)
print(r)
ip = ip.split()
outputString = '' # Added: Initialise an empty string to keep the answer
for word in ip:
if item in word:
outputString += word + ' ' # Changed: Accumulates the answer in a string
print(outputString[:-1]) # Added: Prints all the string's characters
# except the last one, which is the additional space
You're close, just change your print statement from print((word), end=' ') to print((word), end=''). Your print statement has a whitespace for the end but you don't want the whitespace so make the end an empty string.

Print next x lines from string1 until string2

I'm trying to write a function that reads through a text file until it finds a word (say "hello"), then print the next x lines of string starting with string 1 (say "start_description") until string 2 (say "end_description").
hello
start_description 123456 end_description
The function should look like description("hello") and the following output should look like
123456
It's a bit hard to explain. I know how to find the certain word in the text file but I don't know how to print, as said, the next few lines between the two strings (start_description and end_description).
EDIT1:
I found some code which allows to print the next 8, 9, ... lines. But because the text in between the two strings is of variable length, that does not work...
EDIT2:
Basically it's the same question as in this post: Python: Print next x lines from text file when hitting string, but the range(8) does not work for me (see EDIT1).
The input file could look like:
HELLO
salut
A: 123456.
BYE
au revoir
A: 789123.
The code should then look like:
import re
def description(word):
doc = open("filename.txt",'r')
word = word.upper()
for line in doc:
if re.match(word,line):
#here it should start printing all the text between start_description and end_description, for example 123456
return output
print description("hello")
123456
print description("bye")
789123
Here's a way using split:
start_desc = 'hello'
end_desc = 'bye'
str = 'hello 12345\nabcd asdf\nqwer qwer erty\n bye'
print str.split('hello')[1].split('bye')[0]
The first split will result in:
('', ' 12345\nabcd asdf\nqwer qwer erty\n bye')
So feed the second element to the second split and it will result in:
('12345\nabcd asdf\nqwer qwer erty\n ', '')
Use the first element.
You can then use strip() to remove the surrounding spaces if you wish.
def description(infilepath, startblock, endblock, word, startdesc, enddesc):
with open(infilepath) as infile:
inblock = False
name = None
found = False
answer = []
for line in infile:
if found and not inblock: return answer
if line.strip() != startblock and not inblock: continue
if line.strip() == startblock: inblock = True
elif line.strip() == endblock: inblock = False
if not line.startswith(startdesc):
name = line.strip()
continue
if name is not None and name != word: continue
if not line.startswith(startdesc): continue
answer.append(line.strip().lstrip(startdesc).rstrip(enddesc))

Categories

Resources