python won't handle characters - python

I'm writing a code that searches a .json with data about all the Magic the Gathering cards according to many possible different criteria.
It works fine except that whenever a card has characters such as "-" in its text, my code will ignore that text entirely. It is as its text field was blank.
At some point in the code, the cards' texts are appended to a list, and maybe that's where the problem lies, because i made a simple .py file to test something different, and i can get the text.
for i in data:
if data[i]["name"].find(" some name* ")!=-1:
print data[i]["text"]
This is working actually. it prints the text of the card flawlessly.
however, when i switch this to
for i in data:
if data[i]["name"].find(" some name* ")!=-1:
somelist.append(data[i]["text"]
print somelist
i will find the same issue again. i will get the text of cards that don't have characters such as "-" on them.
EDIT: This is an example of text that python will ignore:
"+3: Destroy target noncreature permanent.\n−2: Gain control of target creature.\n−9: Nicol Bolas, Planeswalker deals 7 damage to target player. That player discards seven cards, then sacrifices seven permanents."
This â^'9 should be translated to "-".
Another text example that has been ignored every single time is this:
"Target creature gets -1/-1 until end of turn.\nMorbid — That creature gets -13/-13 until end of turn instead if a creature died this turn."

Related

How to separate words to single letters from text file python

How do I separate words from a text file into single letters?
I'm given a text where I have to calculate the frequency of the letters in a text. However, I can't seem to figure out how I separate the words into single letters so I can count the unique elements and from there determine their frequency.
I apologize for not having the text in a text file, but the following text I'm given:
alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, and what is the use of a book,' thought alice without pictures or conversation?'
so she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy- chain would be worth the trouble of getting up and picking the daisies, when suddenly a white rabbit with pink eyes ran close by her.
there was nothing so very remarkable in that; nor did alice think it so very much out of the way to hear the rabbit say to itself, `oh dear! oh dear! i shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the rabbit actually took a watch out of its waistcoat- pocket, and looked at it, and then hurried on, alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.
in another moment down went alice after it, never once considering how in the world she was to get out again.
the rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that alice had not a moment to think about stopping herself before she found herself falling down a very deep well.
I'm supposed to separate into getting 26 variables a-z, and then determine their frequency which is given as the following:
I tried making the following code so far:
# Check where the current file you are working in, is saved.
import os
os.getcwd()
#print(os.getcwd())
# 1. Change the current working directory to the place where you have saved the file.
os.chdir('C:/Users/Annik/Desktop/DTU/02633 Introduction to programming/Datafiles')
os.getcwd()
#print(os.chdir('C:/Users/Annik/Desktop/DTU/02633 Introduction to programming/Datafiles'))
# 2. Listing the content of current working directory type
os.listdir(os.getcwd())
#print(os.listdir(os.getcwd()))
#importing the file
filein = open("small_text.txt", "r") #opens the file for reading
lines = filein.readlines() #reads all lines into an array
smalltxt = "".join(lines) #Joins the lines into one big string.
import numpy as np
def letterFrequency(filename):
#counts the frequency of letters in a text
unique_elems, counts = np.unique(separate_words, return_counts=True)
return unique_elems
I just don't know how to separate the letters in the text, so I can count the unique elements.
You can use collections.Counter to get your frequencies directly from the text.
Then just select the 26 keys you are interested, because it will also include whitespaces and other signs.
from collections import Counter
[...]
with open("small_text.txt", "r") as file:
text = file.read()
keys = "abcdefghijklmnopqrstuvwxyz"
c = Counter(text.lower())
# initialize occurrence with zeros to have all keys present.
occurrence = dict.fromkeys(keys, 0)
occurrence.update({k:v for k,v in c.items() if k in keys})
total = sum(occurrence.values())
frequency = {k:v/total for k,v in occurrence.items()}
[...]
To handle upper case str.lower might be useful as well.
"how I separate the words into single letters" since you want to calculate the count of the characters you can implement python counter in collections.
For example
import collections
import pprint
...
...
file_input = input('File_Name: ')
with open(file_input, 'r') as info:
count = collections.Counter(info.read().upper()) # reading file
value = pprint.pformat(count)
print(value)
...
...
This read your file will output the count of characters present.

Is there a way to remove items from various lists based on conditional statements?

I am writing a program that will act as a photography idea-generator for New York photographers. The way it works now is quite simple, the code is utilizing the random.choice function to randomly pull items from lists, then the code prints them out in a way that forms a sentence in English as an end result.
My issue is I need to add some logic to this, as some results would not make sense for a photographer to do (at least in my opinion). In this example I am trying to remove 'Bracketed (HDR)' from the technique_list, IF "Portrait" happens to be randomly chosen when python chooses the theme item.
I have a feeling I am mis-using the .remove function within the conditional if statement. Is there a better way to do this? I have attached the pertinent parts of the code for examination.
I have tried technique_list.remove('Bracketed (HDR)') , as well as
del technique_list[0] , both as the response part of the if statement.
import random
print ("You should try taking a...")
#pool of items that the program will randomly choose..
theme_list = ['Cityscape','Peoplescape','Port-Scape', 'Portrait']
technique_list = ['Bracketed (HDR)','Wide Angle', 'Zoom','Long
Exposure','Fast Shutter','Daytime Long Expo','Timelapse']
#what we need here are conditional IF statements, that manipulate items
from various lists
#this bit of code determines the theme of a photo idea
theme_var = random.choice(theme_list)
for theme in theme_var:
if theme == 'Portrait':
technique_list.remove('Bracketed (HDR)')
print("",theme_var)
#this bit of code determines the technique of a photo idea
technique_var = random.choice(technique_list)
print("", technique_var)
print("picture, from")
#this line of code determines the location of a photo idea
location_var = random.choice(location_list)
print("", location_var)
This still remains one of the possible results of the code:
You should try taking a...
Portrait
Bracketed (HDR)
picture, from
34th Street
during
Sunrise
and then give it a
Black & White
edit in Lightroom!
[Finished in 0.2
As I said earlier, Portrait and Bracketed (HDR) should never be part of the same result, it doesn't make sense for this situation.
The issue (I think) is because you are iterating over the randomly chosen result not the list itself, you don't need the for loop that is.
theme_var = random.choice(theme_list)
if theme_var == 'Portrait':
technique_list.remove('Bracketed (HDR)')
print("",theme_var)
#this bit of code determines the technique of a photo idea
technique_var = random.choice(technique_list)
print("", technique_var)
print("picture, from")
#rest of the code
Should do it
I'd go with a dictionary of inappropriate techniques, a list comprehension, and top it off with an f-string:
import random
#pool of items that the program will randomly choose..
theme_list = ['Cityscape','Peoplescape','Port-Scape', 'Portrait']
technique_list = ['Bracketed (HDR)','Wide Angle', 'Zoom','Long Exposure','Fast
Shutter','Daytime Long Expo','Timelapse']
location_list = ['34th Street']
# dictionary of inappropriate techniques for given theme
d_inappropes = {'Cityscape': [],
'Port-Scape': [],
'Portrait': ['Bracketed (HDR)'],
'Peoplescape': ['Long Exposure', 'Timelapse', 'Daytime Long Expo']}
#this bit of code determines the theme of a photo idea
theme_var = random.choice(theme_list)
#this bit of code determines the technique of a photo idea
# list comprehension generates a new list with the inappropriate techniques removed,
# without affecting the original list
technique_var = random.choice([ti for ti in technique_list if ti not in d_inappropes[theme_var]])
#this line of code determines the location of a photo idea
location_var = random.choice(location_list)
# use f-stirngs to put the whole output in a single line to keep it managable
print(f"You should try taking a \n {theme_var} {technique_var} picture, \n from
{location_var}.")
if I may add and give more explanation to the answers
You want to delete "bracked (HDR)" IF potrait is selected. Don't use .remove as it will delete "bracked (HDR)" permanently and prevent other theme to use that technique. you can use dictionary of inappropriate technique as kingfischer suggested for that
random.choice outputted a single value from your list. you should not use for-loop with it as for-loop will iterate over the character/alphabets in the value outputted by random.choice
if I may give a feedback, the indentations in your code snippet are quite jumbled. Some lines that should have indentation, don't have it. I don't know.. maybe it is unintended and the problem is with my browser. if it was so, sorry!

repetition of input in tkinter

im making a grocery list app with tkinter but im having trouble where as telling the computer to reject a repeated grocery. my code so far looks like this
def getlis():
global ind,lastite,ovelim,curite
curite=groent.get()
for x in range(0,len(shotup)):
if curite==shotup[x]:
mylist.insert(ind,shotup[x]+'\t $%.2f'%(pritup[x]))
lastite=shotup[x]
if curite==lastite and ovelim>1:
mylist.insert([ind],'error, already inputted')
t.sleep(1)
mylist.delete(ind)
ovelim+=1
ind+=1
i want the computer to first acept that the item i entered into groent matches up with one of the items in the tuple shotup then print it into a listbox, then recording it into a variable called lastite, the last thing you put on the list, ovelim is just a variable that helps keep track in a way. when ovelim>1 and lastite==curite i want the program to return whats in the second if statement, but all im getting is just a continued recording into my list, ive tried reorganizing, true false and mor ebut nothing seems to work

Please spot the error in my Python code

I have to code for a trouble shooting system, which uses keywords in the user input to provide a solution from a text file. I have a list of If statements, which directs the program to read specific lines from a textile and present them as solutions according to the specific Keywords present in the user input.
However, whatever the user input is, the program seems to display the first two lines in the text file (the program seems to be stuck on the first if statement).
Keywords = ['screen', 'display', 'audio', 'sound',]
Keywords2 = ['cracked', 'broken', 'water', 'frozen', 'unresponsive']
# Collecting the user's problem using user input
problem = input(str("Please enter your problem:")).split()# Assigns the user input to the variable 'progarm' and converts the user input into a string
# Presenting solutions to user if corresponding Keywords are present in the users input 'problem'
if any(i in Keywords or Keywords2 for i in problem): # This while loop checks that any of the keywords (i) in the lists (Keyword or Keywords2) are present in the user's input (problem), which has been splitted into individual words (indicated by spacing - i)
if Keywords[0] or Keywords[1] and Keywords2[0] or Keywords2[1] in problem:
read(0)
read(1)
elif Keywords[0] or Keywords[1] and Keywords2[3] or Keywords2[4] in problem:
read(3)
elif Keywords2[2] in problem:
read(2)
elif Keywords[2] or Keywords[3] and Keywords2[5] or Keywords2[6] in problem:
read(4)
read(0)
When I enter 'the phone fell in water', it should detect the Keyword 'water', which is Keywords2[2], and read the second line in the text file. Instead, it reads the lines directed in the first if statement.
Please enter your problem:My phone fell into water
Get it repaired at a reliable repair shop
You will need to buy a new screen and a new screen protector
Any suggestions to improve the code and make it work would be much appreciated!
I think the problem might be with the improper usage of in.
You may want to do something like this:
any(i in Keywords or i in Keywords2 for i in problem)
instead of
any(i in Keywords or Keywords2 for i in problem)
Similarly, for the inner if and elif statements.
For example:
if (Keywords[0] in problem or Keywords[1] in problem) and (Keywords2[0] in problem or Keywords2[1] in problem):
instead of
if Keywords[0] or Keywords[1] and Keywords2[0] or Keywords2[1] in problem:

Making a program which will read and write to text files

I'm pretty new to Python! I recently started coding a program which I want to write and read to and from text files, while compressing/decompressing sentences (sort of).
However, I've run into a couple problems which I can't seem to fix, basically, I've managed to code the compressing section. But when I go to read the contents of the text file, I'm not sure how to recreate the original sentence through the positions and unique words?!
###This section will compress the sentence(s)###
txt_file = open("User_sentences.txt","wt")
user_sntnce = input(str("\nPlease enter your sentences you would like compressed."))
user_sntnce_list = user_sntnce.split(" ")
print(user_sntnce_list)
for word in user_sntnce_list:
if word not in uq_words:
uq_words.append(word)
txt_file.write(str(uq_words) + "\n")
for i in user_sntnce_list:
positions = int(uq_words.index(i) + 1)
index.append(positions)
print(positions)
print(i)
txt_file.write(str(positions))
txt_file.close()
###This section will DECOMPRESS the sentence(s)###
if GuideChoice == "2":
txt_file = open("User_sentences.txt","r")
contents = txt_file.readline()
words = eval(contents)
print(words)
txt_file.close()
This is my code so far, it seems to work, however as I've said I'm really stuck, and I really don't know how to move on and recreate the original sentence from the text file.
From what understand you want to substitute each word in a text file with a word of your choice (a shorter one if you want to "compress"). Meanwhile you keep a "dictionary" (not in the python sense) uq_words where you associate each different word with an index.
So a sentence "today I like pizza, today is like yesterday" will become:
"12341536".
I tried your code removing if GuideChoice == "2": and defining uq_words=[] and index=[].
If that's what you intend to do then:
I imagine you are calling this compression from time to time, it's in a function. So doing what you do in the second line is to open a NEW file with the same name of the previous ones, meaning you will always have the last sentence compressed, loosing the previous.
Try to read every time the lines, rewrite all and add the new (kinda what you did in contents = txt_file.readline().
You are printing both the compressed translation (like "2345") AND the array whose component are the words of the splitted sentence. I do not think that is the "compressed" document you are aiming for. Just the "2345" part, right?
Since, I believe, you want to keep a dictionary, but this code is inside a function, you will loose the dictionary every time the function ends. So write 2 documents: one with the compressed text (every time refreshed and not rewritten!) and another file with 2 columns, where you write the dictionary. You pass the dictionary file name as a string to the function, so you can update it in case new words are added, and you read it as a NX2 array (N the number of words).

Categories