I want to tell python that everything before a certain character in a string, equals something else. In this code, I want everything after the equals sign to be assigned to a separate list called results, and everything before the equals sign to be assigned to a whole other list called names.
I believe an if/else statement is needed, but I do not know how to signify BEFORE and AFTER in python.
lines = ['Data1 = 100',
'Data2 = TRUE',
'Data3 = 45',
'Data4 = False',
]
You could make use of the list() function. Which splits the string into characters. Your problem with this is that you'll have to either specify the position on number of time the character appears before hand in order to get a start and stop location.
Another option is using the str.replace() if you know exactly what you want to replace, but both of these method present the problem that they are hard coded. If you're only wanting to replace them within a conditional statement, these could be useful.
For your use, you could say something like
loop through array.
split the element in the array using split
add logic statement that if array[i-1] == "=", start replacing elements with new element
then add the new element back into the array at the position you removed wanted to be removed.
Related
I need a function to look up organizational abbreviations in a text and return the first one thas shows up. I tried to solve that with a nested if clause but it has a logical error.
=IF(ISNUMBER(SEARCH("BZC";I5)); "Finanz"; IF(ISNUMBER(SEARCH("AZC" /1";I5));"IT";""))
It looks up BZC and AZC as desired and return the organization name. However, it does no return the first match in a string. Since BZC is the first lookup it will always be returned if it is in the string, eventhoug it might not be the first org abbreviation.
What functionality of excel can be used to solve this issue? I basically need an array of variables that a function should do a look up and return the first one that is found.
Edit:
I tried to implement the formula form Justyna MK. Besides the fact, that I still need to figure out the meaning of the formula (iferror, mid, small) It returns #N/A in my example. Is there a certain reason for that?
I hope I understood your request correctly. Here's an Array formula for you to try (enter using Ctrl + Shift + Enter):
=CHOOSE(MATCH(MID(A1,SMALL(IFERROR(SEARCH({"BZC","AZD","xxx"},A1),""),1),3),{"BZC","AZD","xxx"},0),"Finanz","IT","Other")
You'd probably need to change , to ; in order to match your regional settings.
You can easily expand the list of search items by modifying the contents of curly brackets { } and also by expanding the MATCH results at the very end of the formula.
Here's some sample result:
Edit: here's an adjusted solution that ignores the length of your code (the previous solution was assuming that the code is always 3-characters long). This time it's not an array formula so you can enter it as it is.
Also, I suspect that you should not change , to ; inside the curly brackets (it would modify the formula from column to row delimiter and thus it will stop working). The remaining , can be transformed to ;, if that makes sense.
=CHOOSE(MATCH(TRIM(LEFT(SUBSTITUTE(RIGHT(A1,LEN(A1)-SUMPRODUCT(SMALL(IFERROR(SEARCH({"BZC","AZD","XYZX/1","NP-HSD"},A1),""),1))+1)," ",REPT(" ",255)),255)),{"BZC","AZD","XYZX/1","NP-HSD"},0),"Finanz","IT","Other1","Other2")
The result:
I want get all values of dropdown and want to store them somewhere. from follwing NASDAQ site https://www.nasdaq.com/symbol/ge/historical i want get all values of Timeframe and want to somewhere so that i can use those values one b one in loop and get the values of stock for all timeframe. Click below image screenshot
It's not that easy to get each of the values, but it's not impossible. First you can get all the values in a Data Item as text. If you spy the element, you will notice that the attribute Value contains what you want. So you will need to use a read stage and get this specific attribute's value (you can ignore the PDF elements):
Doing so will give you the following:
The problem with this is that you cannot use this in a loop. One way around would be to split on space:
And the resulting collection (I called it Split Values) will look like this:
But it's not quite there yet. You should however be able to use this collection to get the collection you need (or use it directly).
If you use it directly, I would say it should look like this:
Empty? has the expression [Split Values.words]="" (notice the last row is blank)
Value is number has the expression IsNumber([Split Values.words])
Set Current Item as Number has expression [Split Values.words] with store value Current Item.
Append word to Current Item has expression [Current Item]&" "&[Split Values.words] with store value Current Item.
Let's say i have a list of the alphabet
myList=["a","b","c"..."z"]
Now lets say we have a variable within a loop that takes out a random letter from the list. Obviously random is imported.
while True:
ans=myList[random.randint(1,26)]
I want the user to be asked to take a guess at a letter so within the loop i add
guess=input('Take a guess at a letter from the alphabet')
The user will receive a clue on the whereabouts of the answer
print('The letter locates between x and x.')
Question. How can i determine the position of ans in myList so i can give two random values and perhaps assign them to variables, one below ans and one value over ans.
The range would always be random between these two values so ans is not always the median of the two values.
p.s. I would put the script together to give a better view of what it looks like, but unfortunately i find the formatting help very confusing, and highlighting pieces of code and pressing Ctrl+K does not work as simply as i expected.
The position is the output of the random call, right?
You can save that to a variable before calling the myList[]
index = random.randint(1,26)
ans = myList[index]
use
myList.index(ans)
for above code to work you need to have ans in myList or else it will throw an exception.
BTW this question is similar to Finding the index of an item given a list containing it in Python
Python 3.4
I've got an Excel file with some messy organizing, but one this is for sure:
I need EVERYTHING except the stuff that appears before the very first comma in every single line, the comma included.
Example:
Print command of the file gives me this:
Word1 Funky,Left Side,UDLRDURLUDRUDLUR
Nothing (because not) exists lol extraline,Right
Side,RBRGBRGBRGRBGRBGBR
What I want to get is this:
Left Side,UDLRDURLUDRUDLUR
Right Side,RBRGBRGBRGRBGRBGBR
I'd also like to make that into a dictionary:
dictionary = {"Left Side":"UDLRDURLUDRUDLUR", "Right Side":"RBRGBRGBRGRBGRBGBR",}
So basically I want to get rid of everything until the first comma (comma included), make the second part the key (ends at second comma), and third part the value (line ends with value).
What would be the easiest way to execute this?
Suppose s contains the string to be examined:
s = "word1,Left Side,UDLRDURLUDRUDLUR"
There are a number of ways to get rid of everything up to and including the first comma. You can use
Slicing coupled with find: s[s.find(',')+1:]
This expression will yield the desired result if the string s contain at least one comma, but it will yield the entire string if the string does not contain any commas.
Split coupled with indexing: s.split(',',1)[1]
This expression will yield the desired result if the string s contain at least one comma, but it will raise IndexError if the string does not contain any commas.
Regular expressions, but that's overkill here.
Other techniques, but those are also overkill here.
I have been using nlapiCreateFile() and nlapiSubmitFile() to create a CSV file from an array, and have run in to two problems I cant seem to figure out. When the CSV file is saved excel prints out each element of the array in to its own cell like it should but it prints it all on the same row (1a, 1b, 1c, 1d.. etc) I would rather have the array print downwards in the same column rather then row(1a, 2a, 3a, 4a... etc) if possible. But im not sure how to approach this.
var file1 = nlapiCreateFile('names.csv', 'CSV', names);
file1.setFolder(295767);
nlapiSubmitFile(file1);
The second thing I cant seem to figure out, if I wanted to print a second array in the same file, how would I approach that? For example the names array in the first columns and another array in the 2nd column.
Instead of using an Array for the third parameter of nlapiCreateFile(), try using a string that uses the , for column delimiters, and \n as a line separator.
I know this is rather old, but for those who are trying to figure this out, you want to use the join method:
var file1 = nlapiCreateFile('names.csv', 'CSV', names.join("\n"));
file1.setFolder(295767);
nlapiSubmitFile(file1);
This returns a string of the array, with the elements separated by the provided parameter. In this case, we want one column with all of the elements so we choose \n or new line.
For appending to a file, I believe you will have to load the record again, which will return and nlobjFile Object https://debugger.sandbox.netsuite.com/app/help/helpcenter.nl?fid=section_N3066995.html#bridgehead_N3067099
Then you can add to it, and then submit again.