I am switching from Python Console to a Python App/Programm.
I was using for loops with many prints.
I switched to PyQT5 with an Output Text aka. as Label
here is an example of my for loops:
i = 0
for x in data['data']:
rank = data['data'][i]["currenttierpatched"]
rr = data['data'][i]["mmr_change_to_last_game"]
date = data['data'][i]["date"]
print(f"{date}\n{rank} {rr}rr")
i += 1
I only have one Output Text, now how can i like get all the things i get from the for loop with only one command so i have one line of code for the Output Text?
Because i cant get the for loop in one single Text, you know what i mean?
because the Person from a comment doesnt answer this Post.
Im gonna answer it.
I simply made a List with
list = []
and instead of printing in every for loop i used list.append("Text")
at the end i made a new variable called Text and used "".join(list)
then in my Output Lable i simply used:
self.DOutputText.setText(f"{text}")
So it would be:
list = []
if data['status'] == 200:
for x in data['data']:
rank = data['data'][i]["currenttierpatched"]
rr = data['data'][i]["mmr_change_to_last_game"]
date = data['data'][i]["date"]
list.append(f"{date}\n{rank} {rr}rr\n")
i += 1
text = "".join(list)
self.COutputText.setText(f"{text}")
else:
self.COutputText.setText(f"Error | {data['status']}\n{data['message']}")
Thanks for you help!
EDIT
With the comment from wyattg71, i removed the i variable and edited the for loop.
list = []
if data['status'] == 200:
for i, x in data['data']:
rank = data['data'][i]["currenttierpatched"]
rr = data['data'][i]["mmr_change_to_last_game"]
date = data['data'][i]["date"]
list.append(f"{date}\n{rank} {rr}rr\n")
text = "".join(list)
self.COutputText.setText(f"{text}")
else:
self.COutputText.setText(f"Error | {data['status']}\n{data['message']}")
Related
I am doing AOC 2020 and I am seeing something weird with python3 print statement and I don't understand what's going on.
This question is day 6 part 1 and it is suppose to count a bunch of chars in a file and basically find out how many is counted.
This is my code below, and when I run it on my Pycharm 2020.3 in Win 10 with Python 3.5, these two print statements gives wildly different results:
The print(a) line gives me what I expect, the value returned from process_customs_data.
The print(process_customs_data(customs_data)) gives me 0 which makes no sense shouldn't it print the return value of the function?
and just as an extra test I changed total_count =total_count + a with total_count =total_count + process_customs_data(customs_data) and both scenarios works. I can see my total count updated correctly.
import pprint
from aocd.models import Puzzle
from aocd import submit
import time
def process_customs_data(data):
pprint.pprint(set(data))
return len(set(data))
puzzle = Puzzle(year=2020, day=6)
raw = puzzle.input_data
data = raw.splitlines()
total_count = 0
customs_data = ""
for line in data:
if line == '':
a = process_customs_data(customs_data)
total_count =total_count + a
customs_data = ''
print(a)
print(process_customs_data(customs_data))
time.sleep(5)
else:
customs_data = customs_data + line.strip()
print(total_count)
#submit(total_count, part="a", day=6, year=2020)
You print(process_customs_data(customs_data)) command prints the actual return value of the expression process_customs_data(customs_data).
And if you are wondering, why that's different from a, then just see that you updated function argument customs_data = '' before calling your function.
In your function process_customs_data(data) you create a set that will contain all possible different characters from your string. For the call with the empty data it will be the empty set, so the len of the empty set is 0. And print prints this 0.
What I am struggling with now a bit, is getting a set of code to run, where I can automate the cycle of chosen lists, to assign different variables to different lists.
I am using .append, but what I am aiming to do is the following:
x.append(y), I'd want the x list used to be chosen from a set that I have, another list of the list names for example. I have made the following code to capture this:
temp_stat_list = ["10Fouls5","2Corners4","5Crosses6","80Touches92"]
stat_picker = 0
extra_stats = ["Fouls","Corners","Crosses","Touches"]
stat_list = ["foul_list","corner_list","cross_list","touch_list"]
stat_list_len = len(stat_list) - 1
while stat_picker <= stat_list_len:
working_stat = extra_stats[stat_picker]
working_list = stat_list[stat_picker]
stat_len = len(working_stat)
add_stat_home = temp_stat_list[stat_picker]
add_stat_home = add_stat_home[:add_stat_home.find(working_stat)]
stat_list[stat_picker].append(int(add_stat_home))
add_stat_away = temp_stat_list[stat_picker]
add_stat_away = add_stat_away[(add_stat_away.find(working_stat) + stat_len):]
stat_list[stat_picker].append(int(add_stat_away))
stat_picker = stat_picker + 1
Ideally in this code the part:
stat_list[stat_picker].append(int(add_stat_home))
stat_list[stat_picker].append(int(add_stat_away))
the stat list portion would move with the stat picker increase at the end. Effectively capturing the variables in the first run, then moving to the next, and adjusting the list accordingly. Thanks!
I've been successfully implemented this chunk with the different instances of the lists, running individually. This of course adds a fair bit of repetitive code, and I am wanting to run a simple while loop to just automate this.
I can get it to work with just manually running each list instance separately, but as a loop, I can't get it to work.
EDIT: Added temp_stat_list example content.
So, I deleted my other post because I realized I accidentally asked the question as an answer, instead of a comment.
Here's what I think you're looking for, let me know if this is it:
import re
temp_stat_list = ["10Fouls5","2Corners4","5Crosses6","80Touches92"]
fouls = {'home':[],
'away':[]}
corners = {'home':[],
'away':[]}
crosses = {'home':[],
'away':[]}
touches = {'home':[],
'away':[]}
for stat in temp_stat_list:
parsed_stats = re.match(r"([0-9]+)([a-z]+)([0-9]+)", stat, re.I)
stats = parsed_stats.groups()
if stats[1] == "Fouls":
fouls['home'].append(stats[0])
fouls['away'].append(stats[2])
elif stats[1] == "Corners":
corners['home'].append(stats[0])
corners['away'].append(stats[2])
elif stats[1] == "Crosses":
crosses['home'].append(stats[0])
crosses['away'].append(stats[2])
else:
touches['home'].append(stats[0])
touches['away'].append(stats[2])
print("Fouls: ")
print(fouls)
print("Corners: ")
print(corners)
print("Crosses: ")
print(crosses)
print("Touches: ")
print(touches)
This gives an output of:
Fouls:
{'home': ['10'], 'away': ['5']}
Corners:
{'home': ['2'], 'away': ['4']}
Crosses:
{'home': ['5'], 'away': ['6']}
Touches:
{'home': ['80'], 'away': ['92']}
Ok so the answer was a lot simpler than I thought, just needed another set of eyes. Really all we needed to do, was use a list of lists, so then the final code that worked perfectly, is the following:
stat_picker = 0
extra_stats = ["Fouls","Corners","Crosses","Touches"]
stat_list = [foul_list,corner_list,cross_list,touch_list]
stat_list_len = len(stat_list) - 1
while stat_picker <= stat_list_len:
working_stat = extra_stats[stat_picker]
working_list = stat_list[stat_picker]
stat_len = len(working_stat)
add_stat_home = temp_stat_list[stat_picker]
add_stat_home = add_stat_home[:add_stat_home.find(working_stat)]
stat_list[stat_picker].append(int(add_stat_home))
add_stat_away = temp_stat_list[stat_picker]
add_stat_away = add_stat_away[(add_stat_away.find(working_stat) + stat_len):]
stat_list[stat_picker].append(int(add_stat_away))
stat_picker = stat_picker + 1
The change here is that the stat_list needed to be a list of lists instead. A list of strings wouldn't be recognized as lists being applied.
(Code below)
I'm scraping a website and the data I'm getting back is in 2 multi-dimensional arrays. I'm wanting everything to be in a JSON format because I want to save this and load it in again later when I add "tags".
So, less vague. I'm writing a program which takes in data like what characters you have and what missions are requiring you to do (you can complete multiple at once if the attributes align), and then checks that against a list of attributes that each character fulfills and returns a sorted list of the best characters for the context.
Right now I'm only scraping character data but I've already "got" the attribute data per character - the problem there was that it wasn't sorted by name so it was just a randomly repeating list that I needed to be able to look up. I still haven't quite figured out how to do that one.
Right now I have 2 arrays, 1 for the headers of the table and one for the rows of the table. The rows contain the "Answers" for the Header's "Questions" / "Titles" ; ie Maximum Level, 50
This is true for everything but the first entry which is the Name, Pronunciation (and I just want to store the name of course).
So:
Iterations = 0
While loop based on RowArray length / 9 (While Iterations <= that)
HeaderArray[0] gives me the name
RowArray[Iterations + 1] gives me data type 2
RowArray[Iterations + 2] gives me data type 3
Repeat until Array[Iterations + 8]
Iterations +=9
So I'm going through and appending these to separate lists - single arrays like CharName[] and CharMaxLevel[] and so on.
But I'm actually not sure if that's going to make this easier or not? Because my end goal here is to send "CharacterName" and get stuff back based on that AND be able to send in "DesiredTraits" and get "CharacterNames who fit that trait" back. Which means I also need to figure out how to store that category data semi-efficiently. There's over 80 possible categories and most only fit into about 10. I don't know how I'm going to store or load that data.
I'm assuming JSON is the best way? And I'm trying to keep it all in one file for performance and code readability reasons - don't want a file for each character.
CODE: (Forgive me, I've never scraped anything before + I'm actually somewhat new to Python - just got it 4? days ago)
https://pastebin.com/yh3Z535h
^ In the event anyone wants to run this and this somehow makes it easier to grab the raw code (:
import time
import requests, bs4, re
from urllib.parse import urljoin
import json
import os
target_dir = r"D:\00Coding\Js\WebScraper" #Yes, I do know that storing this in my Javascript folder is filthy
fullname = os.path.join(target_dir,'TsumData.txt')
StartURL = 'http://disneytsumtsum.wikia.com/wiki/Skill_Upgrade_Chart'
URLPrefix = 'http://disneytsumtsum.wikia.com'
def make_soup(url):
r = requests.get(url)
soup = bs4.BeautifulSoup(r.text, 'lxml')
return soup
def get_links(url):
soup = make_soup(url)
a_tags = soup.find_all('a', href=re.compile(r"^/wiki/"))
links = [urljoin(URLPrefix, a['href'])for a in a_tags] # convert relative url to absolute url
return links
def get_tds(link):
soup = make_soup(link)
#tds = soup.find_all('li', class_="category normal") #This will give me the attributes / tags of each character
tds = soup.find_all('table', class_="wikia-infobox")
RowArray = []
HeaderArray = []
if tds:
for td in tds:
#print(td.text.strip()) #This is everything
rows = td.findChildren('tr')#[0]
headers = td.findChildren('th')#[0]
for row in rows:
cells = row.findChildren('td')
for cell in cells:
cell_content = cell.getText()
clean_content = re.sub( '\s+', ' ', cell_content).strip()
if clean_content:
RowArray.append(clean_content)
for row in rows:
cells = row.findChildren('th')
for cell in cells:
cell_content = cell.getText()
clean_content = re.sub( '\s+', ' ', cell_content).strip()
if clean_content:
HeaderArray.append(clean_content)
print(HeaderArray)
print(RowArray)
return(RowArray, HeaderArray)
#Output = json.dumps([dict(zip(RowArray, row_2)) for row_2 in HeaderArray], indent=1)
#print(json.dumps([dict(zip(RowArray, row_2)) for row_2 in HeaderArray], indent=1))
#TempFile = open(fullname, 'w') #Read only, Write Only, Append
#TempFile.write("EHLLO")
#TempFile.close()
#print(td.tbody.Series)
#print(td.tbody[Series])
#print(td.tbody["Series"])
#print(td.data-name)
#time.sleep(1)
if __name__ == '__main__':
links = get_links(StartURL)
MainHeaderArray = []
MainRowArray = []
MaxIterations = 60
Iterations = 0
for link in links: #Specifically I'll need to return and append the arrays here because they're being cleared repeatedly.
#print("Getting tds calling")
if Iterations > 38: #There are this many webpages it'll first look at that don't have the data I need
TempRA, TempHA = get_tds(link)
MainHeaderArray.append(TempHA)
MainRowArray.append(TempRA)
MaxIterations -= 1
Iterations += 1
#print(MaxIterations)
if MaxIterations <= 0: #I don't want to scrape the entire website for a prototype
break
#print("This is the end ??")
#time.sleep(3)
#jsonized = map(lambda item: {'Name':item[0], 'Series':item[1]}, zip())
print(MainHeaderArray)
#time.sleep(2.5)
#print(MainRowArray)
#time.sleep(2.5)
#print(zip())
TsumName = []
TsumSeries = []
TsumBoxType = []
TsumSkillDescription = []
TsumFullCharge = []
TsumMinScore = []
TsumScoreIncreasePerLevel = []
TsumMaxScore = []
TsumFullUpgrade = []
Iterations = 0
MaxIterations = len(MainRowArray)
while Iterations <= MaxIterations: #This will fire 1 time per Tsum
print(Iterations)
print(MainHeaderArray[Iterations][0]) #Holy this gives us Mickey ;
print(MainHeaderArray[Iterations+1][0])
print(MainHeaderArray[Iterations+2][0])
print(MainHeaderArray[Iterations+3][0])
TsumName.append(MainHeaderArray[Iterations][0])
print(MainRowArray[Iterations][1])
#At this point it will, of course, crash - that's because I only just realized I needed to append AND I just realized that everything
#Isn't stored in a list as I thought, but rather a multi-dimensional array (as you can see below I didn't know this)
TsumSeries[Iterations] = MainRowArray[Iterations+1]
TsumBoxType[Iterations] = MainRowArray[Iterations+2]
TsumSkillDescription[Iterations] = MainRowArray[Iterations+3]
TsumFullCharge[Iterations] = MainRowArray[Iterations+4]
TsumMinScore[Iterations] = MainRowArray[Iterations+5]
TsumScoreIncreasePerLevel[Iterations] = MainRowArray[Iterations+6]
TsumMaxScore[Iterations] = MainRowArray[Iterations+7]
TsumFullUpgrade[Iterations] = MainRowArray[Iterations+8]
Iterations += 9
print(Iterations)
print("It's Over")
time.sleep(3)
print(TsumName)
print(TsumSkillDescription)
Edit:
tl;dr my goal here is to be like
"For this Mission Card I need a Blue Tsum with high score potential, a Monster's Inc Tsum for a bunch of games, and a Male Tsum for a long chain.. what's the best Tsum given those?" and it'll be like "SULLY!" and automatically select it or at the very least give you a list of Tsums. Like "These ones match all of them, these ones match 2, and these match 1"
Edit 2:
Here's the command Line Output for the code above:
https://pastebin.com/vpRsX8ni
Edit 3: Alright, just got back for a short break. With some minor looking over I see what happened - my append code is saying "Append this list to the array" meaning I've got a list of lists for both the Header and Row arrays that I'm storing. So I can confirm (for myself at least) that these aren't nested lists per se but they are definitely 2 lists, each containing a single list at every entry. Definitely not a dictionary or anything "special case" at least. This should help me quickly find an answer now that I'm not throwing "multi-dimensional list" around my google searches or wondering why the list stuff isn't working (as it's expecting 1 value and gets a list instead).
Edit 4:
I need to simply add another list! But super nested.
It'll just store the categories that the Tsum has as a string.
so Array[10] = ArrayOfCategories[Tsum] (which contains every attribute in string form that the Tsum has)
So that'll be ie TsumArray[10] = ["Black", "White Gloves", "Mickey & Friends"]
And then I can just use the "Switch" that I've already made in order to check them. Possibly. Not feeling too well and haven't gotten that far yet.
Just use the with open file as json_file , write/read (super easy).
Ultimately stored 3 json files. No big deal. Much easier than appending into one big file.
I am using python and some arcpy modules (that I do not think are important for my question) to run a code containing three iterations (a loop with two smaller loops inside). My code looks like this:
file = r"C:Directory\File"
cursor = arcpy.SearchCursor(file,fields)
A = range(1,270)
os.chdir ('C:\Directory')
book = xlwt.Workbook()
sheet1 = book.add_sheet('Sheet1',cell_overwrite_ok = True)
for a in A:
tot = 0
result = 0
for row in cursor:
if row.getValue('Firstfield') == a:
tot = tot + row.getValue('secondfield')
print (tot)
for row in cursor:
print row.getValue('GTYS_Branc')
if row.getValue('GTYS_Branc') == a:
result= result + row.getValue(Field1) * ((row.getValue(Field2))/tot )
sheet1.write((branch-1),0,finalIIIprob)
sheet1.write((a-1),1,result)
book.save('Excel.xls')
The code works but does not give me the results I want. I noticed that the problem is that it practically ignores the second internal loop and every iteration of the first internal loop after the first one. What could the problem be?
The issue may be that you are iterating over values in an "array", but there is nothing in there but one number which is A, not all the numbers from 0 to A, what you need to do is use range function:
try for i in range(A):
This piece of code in theory have to compare two lists which have the ID of a tweet, and in this comparison if it already exists in screen printing , otherwise not.
But I print all or not being listed.
Any suggestions to compare these two lists of ID's and if not the ID of the first list in the second then print it ?
Sorry for the little efficient code . ( and my English )
What I seek is actually not do RT ( retweet ) repeatedly when I already have . I use Tweepy library , I read the timeline , and make the tweet RT I did not do RT
def analizarRT():
timeline = []
temp = []
RT = []
fileRT = openFile('rt.txt')
for status in api.user_timeline('cnn', count='6'):
timeline.append(status)
for i in range(6):
temp.append(timeline[i].id)
for a in range(6):
for b in range(6):
if str(temp[a]) == fileRT[b]:
pass
else:
RT.append(temp[a])
for i in RT:
print i
Solved add this function !
def estaElemento(tweetId, arreglo):
encontrado = False
for a in range(len(arreglo)):
if str(tweetId) == arreglo[a].strip():
encontrado = True
break
return encontrado
Its a simple program, don't complicate it. As per your comments, there are two lists:)
1. timeline
2. fileRT
Now, you want to compare the id's in both these lists. Before you do that, you must know the nature of these two lists.
I mean, what is the type of data in the lists?
Is it
list of strings? or
list of objects? or
list of integers?
So, find out that, debug it, or use print statements in your code. Or please add these details in your question. So, you can give a perfect answer.
Mean while, try this:
if timeline.id == fileRT.id should work.
Edited:
def analizarRT():
timeline = []
fileRT = openFile('rt.txt')
for status in api.user_timeline('cnn', count='6'):
timeline.append(status)
for i in range(6):
for b in range(6):
if timeline[i].id == fileRT[b].id:
pass
else:
newlist.append(timeline[i].id)
print newlist
As per your question, you want to obtain them, right?. I have appended them in a newlist. Now you can say print newlist to see the items
your else statement is associated with the for statement, you probably need to add one more indent to make it work on the if statement.