python spliting of string only first element accessable - python

I've googled around a bit and it seems like nobody has had this problem before so here we go:
Function:
def split_time(time_string):
time = time_string.split('T')
time_array = time[-1]
return time_array
Call of Function:
class Entry():
def __init__(self,start,end,summary,description):
self.start_date = split_time(start)
self.end_date = split_time(end)
self.summary = summary
self.description = description
My function recieves a string containing a date time format like this: 2018-03-17T09:00:00+01:00
I want to cut it at 'T' so i used time = time_string.split('T') which worked just fine!
The output of time is ['2018-05-08', '12:00:00+02:00'].
So now i wanted to split it some more and ran into the following error:
While i can access time[0] which delivers the output 2018-05-08 i cant access time[1], i just get an Index out of range Error.
To me it seems like time does contain an array with two strings inside because of its output yo i'm really at a loss right now.
Any help would be appreciated =)
(and an explanation too!)

Use item[-1] to access the last item in the last.
Still unsure why item[1] would throw an error for a list with two items in it.

Related

Converting time to take away

Hiya so I have a data frame which has the time something occurs in one column and the time that it ends in the next column. I need to try and find the time difference between the two, but theyre both strings so it wont simply let me compare them, is there a way I can change them to ints (theyre in the format HH:MM:SS) I found a way to split them using .split (I've put what I did for the original time below, the I could do the same for the second column and work them out from there, but I was wondering if there was an easier way?
...
TIA!
q = 0
for int in range(long):
intel = df_data_bad_time1.loc[q,'Time']
H_M_S = intel.split(':')
df_data_bad_time1.loc[q,'Hours'] = H_M_S[0]
df_data_bad_time1.loc[q,'Mins'] = H_M_S[1]
df_data_bad_time1.loc[q,'Secs'] = H_M_S[2]
q = q + 1
df_data_bad_time1['Hours'] = pd.to_numeric(df_data_bad_time1['Hours'], errors='coerce').astype('Int64')
df_data_bad_time1['Mins'] = pd.to_numeric(df_data_bad_time1['Mins'], errors='coerce').astype('Int64')
df_data_bad_time1['Secs'] = pd.to_numeric(df_data_bad_time1['Secs'], errors='coerce').astype('Int64')
df_data_bad_time1.head(15)
Here's a simple function I wrote, you can take a look at it and tell me if you don't understand anything:
https://repl.it/#d4nieldev/subtract-time

Python, using IN operator keeps returning false

I'm trying to find a substring within a dataframe object. I'm turning the dataframe object into a string before i do this.
Even though i know for sure that the substring exists in the dataframe object, the "in" operator keeps returning false.
I've spent hours trying to figure out how else i can do this. I've also tried using
df1.str.contains
but it errors out.
Can someone please let me know what i'm doing wrong? I'm willing to try different approaches if necessary.
Here is the code i'm using:
changesfull = r'C:\Users\user\Desktop\changes\changes.xlsx'
locations = pd.read_excel(changesfull)
for change in changes:
y = y+1
z = z+1
g = g+1
df1 = locations.iloc[y:z,2:3]
newframe = "product" in str(df1)
print(newframe)
When i print out str(df1), I get an output of dataframe object:
"1 Effective July 15, 2018 - the following produc..."
Could it be that its unable to find the word product because it doesn't acutally print the full thing? eg. it should be " - the following product is available"
In case this actually helps anyone in the future:
The issue was with my my iloc i was using.
I changed that portion to the following and it worked:
df1 = locations.iloc[y,2]

Python .find() to return the end of found string

So I want to pick some data out of a text file, which looks like this:
##After some other stuff which could change
EASY:[5,500]
MEDIUM:[10,100]
HARD:[20,1000]
EXPERT:[30,2000]
EXTREME:[50,5000]
I'm writing a function which uses the difficulty ('EASY' 'HARD' e.t.c) to return the following list. My current code looks like this:
def setAI(difficulty): #difficulty='EASY' or 'HARD' or...e.t.c)
configFile=open('AISettings.txt')
config=configFile.read()
print(config[(config.find(difficulty)):(config.find(']',(config.find(difficulty))))]) #So it will return the chunk between the difficulty, and the next closed-square-bracket after that
This produces the following output:
>>> HARD:[20,1000
I tried fixing it like this:
print(config[(config.find(difficulty)+2):(config.find(']',(config.find(difficulty)+2))+1)])
which returns:
>>>RD:[20,1000]
The issue I'm trying to adress is that I want it to start after the colon, I am aware that I could use the length of the difficulty string to solve this, but is there a simpler way of returning the end of the string when using the .find() command?
P.S: I couldn't find any duplicates for this, but it is a slightly odd question, so sorry if it's already on here somewhere; Thanks in advance
EDIT: Thanks for the replies, I think you basically all solved the problem, but the chosen answer was becasue I like the iteration line-by-line idea, Cheers guys :)
Well if the file look like this, why not just iterate line by line and do something like:
def setAI(difficulty): #difficulty='EASY' or 'HARD' or...e.t.c)
configFile=open('AISettings.txt')
config=configFile.readlines()
for line in config:
if line.startswith(difficulty.upper()):
print(line[len(difficulty) + 1:])
Find returns the location. But ranges assume that their end number should not be included. Just add one to the end.
config = """
##After some other stuff which could change
EASY:[5,500]
MEDIUM:[10,100]
HARD:[20,1000]
EXPERT:[30,2000]
EXTREME:[50,5000]
"""
difficulty = 'HARD'
begin = config.find(difficulty)
end = config.find(']', begin)
print(config[begin:end+1])
The function find will always give you the position of the first letter of the string. Also consider that the notation string[start:end] will give you the substring including the character at start but excluding the character at end. Therefore you could use something like the following:
def setAI(difficulty):
configFile = open('AISettings.txt')
config = configFile.read()
start = config.find(difficulty) + len(difficulty) + 1
end = config.find(']', start) + 1
print(config[start:end])

Assigning multiple return variables, but one at a time?

I am trying to parse some data and format using nltk, but I can't seem to assign multiple returns to multiple variables over a function iteration (see def preprocess function below.) I tried rewriting my code, which usually leads to a big debug, but it seems I am hitting my head against a Python wall that is intentionally there.
def get_7text():
with open('parsed_text/Larrys Pizza & Sports Parlor_text.csv','r') as file:
reader = csv.reader(file)
dict = [row for row in reader]
file.close()
my_dict = [l[0] for l in dict]
text= my_dict[0]
new_dict=ast.literal_eval(text)
for k,v in new_dict.items():
exec(k + '=v')
return Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
def preprocess():
for day in Days:
day = str(day)
day = sent_tokenize(day)
day = [word_tokenize(s.lower()) for s in day]
day = [pos_tag(s) for s in day]
return day
#code here
Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday = get_7text()
Days=[Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday]
Days=preprocess()
Get7text() returns 7 strings which I can successfully assign. I used to first have it return a dictionary of 7 keys, but formatting was annoying for POS tagging, etc. for NLTK.
The problem is this. Whenever I run preprocess, the program only keeps the 1st item in the list and forgets the other 6. I am trying to force the function to assign each returned output to a list of variables called Days, but to no avail. I also noticed that AFTER Days=preprocess(), Days loses all but the first element (Tuesday through Sunday are empty list of 1 string). However, Days[3] or Days[5] prints the expected data correctly.
I'm expecting there's a better method of representation out there. There are no posts online mentioning it, and it seemed like a sketchy thing to do anyway.
Whenever python sees 'return' it says, "Oh, return, you must be done with your code. I'll stop the function now." As such, it stops after the first iteration. Instead, what you should do is :
def preprocess():
retList = []
for day in Days:
day = str(day)
day = sent_tokenize(day)
day = [word_tokenize(s.lower()) for s in day]
day = [pos_tag(s) for s in day]
retList.append(day)
return (retList)
Naturally, if this doesn't work, then I missed something and we're both at a loss.

Maya Python skinCluster return type not string?

I'm trying to check if an object has a skinCluster on it. My code is pretty basic. Here's an example:
cmds.select(d=True)
joint = cmds.joint()
skinnedSphere = cmds.polySphere(r=2)
notSkinnedSphere = cmds.polySphere(r=2)
skinTestList = [skinnedSphere, notSkinnedSphere]
# Bind the joint chain that contains joint1 to pPlane1
# and assign a dropoff of 4.5 to all the joints
#
cmds.skinCluster( joint, skinnedSphere, dr=4.5)
for obj in skinTestList:
objHist = cmds.listHistory(obj, pdo=True)
skinCluster = cmds.ls(objHist, type="skinCluster")
if skinCluster == "":
print(obj + " has NO skinCluster, skipping.")
else:
print obj, skinCluster
#cmds.select(obj, d=True)
My issue is that even if it can't find a skincluster, it still prints out the "obj, skincluster" rather than the error that it can't find a skinCluster.
I thought a skinCluster returns a string. So if the string is empty, it should print out the error rather than "obj, skincluster".
Any help would be appreciated!
This is a classic Maya issue -- the problem is that Maya frequently wants to give you lists, not single items, even when you know the result ought to be a single item. This means you end up writing a bunch of code to either get one item from a one-item list or to avoid errors that come from trying to get an index into an empty list.
You've got the basics, it's the == "" which is messing you up:
for obj in skinTestList:
objHist = cmds.listHistory(obj, pdo=True)
skinCluster = cmds.ls(objHist, type="skinCluster") or [None]
cluster = skinCluster[0]
print obj, cluster
The or [None] guarantees that you'll always get a list with something in it so it's safe to use the [0] to get the single value. None is a good return value here because (as pointed out in the comments) you can if cluster: and skip empty values.

Categories