Python String Slicing from known word in string - python

I'm trying to read a variable which is written by another function (outside of my control), to look for the presence of a known word and then to copy a sub string beginning at the known word and ending either at the end of the line or a | delimiter.
So I want to write to this variable based on a simple if statement I've written, but at the moment it doesn't take any consideration of what is already in the variable and it needs to. As the code I'm writing makes use of all sorts of aliases, I've tried to simplify what I am doing below
So, firstly the variable 'devices' is written to elsewhere but available to me.
I'm reading another variable 'area' which if specifically set to '3', I need to write the variable 'devices' with the string of 'box2|box3' (or 'box3|box2' - it doesn't matter) and I can ignore the existing content of 'devices' UNLESS it contains 'box1' in the string. It may appear anywhere within the string and will also be appended by other data, but it always either finishes at the end of the line, OR by a | delimiter. So I need to read the entire variable, look for the presence of 'box1' and read as many characters into another variable up until the end of the line of it hits the | delimiter.
The only code I can really share here is this:
area = "3"
if area == "3":
devices = "box2|box3"
print devices
Let's say that 'devices' contains 'box5|box6_standard|box9|box8_ex_345|box1_182', I need to extra box1_182 from that string (and append it back in when I write 'devices' variable - I don't need to worry about any other pre-existing content of that variable.
As another example, the existing 'devices' variable may contain 'box7|box1_345|box6|box8_ex_345', in this case, I'd need to take 'box1_345' and append it to the devices string before I write 'box2|box3' to it ('box2|box3|box1_182')

I'd use regex and the in word to check if devices has 'box1'. If so then simply append it to the new devices string
import re
devices='box5|box6_standard|box9|box8_ex_345|box1_182'
area = "3"
if area == "3":
tdevices = "box2|box3"
if 'box1' in devices:
t=re.search('box1[0-9_:a-zA-Z]*', devices)
devices=tdevices+'|'+t.group()
else:
devices=tdevices
print devices

Related

Python - How to repeatedly concatenate a string into a larger string

So I have a script which reaches out to an API and pulls a bunch of host names. For example, look at the 5 hosts below. (For reference, typical return is anywhere from a few hundred to a few thousand host names,)
b4aa2exxxbf6e95a3225,
9a851xxx2a4fee07s439c,
e17b87exxx8250dc949,
3252dbxxxcabd8a568b3,
6c3029c212ae4xxx31d06,
But here's my problem. When these host names are returned to me, they are formatted as one big string. For every hostname I need to remove the "," and concatenate the following string where the comma used to be,
&ids=, so that I can pass this string along as a parameter to another API endpoint.
If anyone knows a simple way to get this done, I would love some help!
you can use the replace method.
baseString = 'b4aa2exxxbf6e95a3225,9a851xxx2a4fee07s439c,e17b87exxx8250dc949,3252dbxxxcabd8a568b3,6c3029c212ae4xxx31d06,'
newString = baseString.replace(',', '&ids=')
if you have one too much comma at the end, simply remove the last character from the string.

How to create variables for substitution based on user for unique filepath in python?

I'm writing code that I want to make generic to whoever needs to follow it.
Part of the code is reading in an excel file that the user has to download. I know that each user has a specific 6-digit unique ID, and the folder and name of the file remains the same. Is there some way for me to modify the pd.read_csv function so that it is like this:
USERID = '123abc'
pd.read_csv(r'C:\Users\USERID\Documents\Dataset.csv')
I keep getting stuck because there is an ' next to the r so concatenation with a constant does not seem to work.
Similarly, is there a method for code for exporting that would insert the current date in the title?
What you want to use are formatted strings. The r preceding the string literal in your code denotes that you are creating a raw string, which means that you aren't going to ever see the value of your variable get assigned correctly within that string. Python's docs explain what these raw strings are:
Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. (3.10.4 Python Language Reference, Lexical Analysis)
Like Fredericka mentions in her comment, the formatted string is a great way to accomplish what you're trying to do. If you're using Python version 3.6 or greater, you can also use the format method on the string, which does the same thing.
# set the User ID
user_id = "PythonUser1"
# print the full filepath
print("C:\\Users\\{}\\Documents\\Dataset.csv".format(user_id))
# read the CSV file using formatted string literals
my_csv = pd.read_csv("C:\\Users\\{user_id}\\Documents\\Dataset.csv")
# read the CSV file using the format method
my_csv = pd.read_csv("C:\\Users\\{}\\Documents\\Dataset.csv".format(user_id))
For more information, I'd recommend checking out the official Python docs on input and output.

Write list variable to file

I have a .txt file of words I want to 'clean' of swear words, so I have written a program which checks each position, one-by-one, of the word list, and if the word appears anywhere within the list of censorable words, it removes it with var.remove(arg). It's worked fine, and the list is clean, but it can't be written to any file.
wordlist is the clean list.
newlist = open("lists.txt", "w")
newlist.write(wordlist)
newlist.close()
This returns this error:
newlist.write(wordlist)
TypeError: expected a string or other character buffer object
I'm guessing this is because I'm trying to write to a file with a variable or a list, but there really is no alternate; there are 3526 items in the list.
Any ideas why it can't write to a file with a list variable?
Note: lists.txt does not exist, it is created by the write mode.
write writes a string. You can not write a list variable, because, even though to humans it is clear that it should be written with spaces or semicolons between words, computers do not have the free hand for such assumptions, and should be supplied with the exact data (byte wise) that you want to write.
So you need to convert this list to string - explicitly - and then write it into the file. For that goal,
newlist.write('\n'.join(wordlist))
would suffice (and provide a file where every line contains a single word).
For certain tasks, converting the list with str(wordlist) (which will return something like ['hi', 'there']) and writing it would work (and allow retrieving via eval methods), but this would be very expensive use of space considering long lists (adds about 4 bytes per word) and would probably take more time.
If you want a better formatting for structural data you can use built-in json module.
text_file.write(json.dumps(list_data, separators=(',\n', ':')))
The list will work as a python variable too. So you can even import this later.
So this could look something like this:
var_name = 'newlist'
with open(path, "r+", encoding='utf-8') as text_file:
text_file.write(f"{var_name} = [\n")
text_file.write(json.dumps(list_data, separators=(',\n', ':')))
text_file.write("\n]\n")

How to Read A String Literal Stored in a Variable

I was wondering if there was a way to read a string literal stored in a variable. I was essentially trying to extract the file name for a variable containing a file path. I'm aware that you need to place r' before the path name. In my example below, the variable I'm trying to update is 'test'. So basically I'm unaware of how I can use r' on the variable name to avoid parts of the path being read as unicode characters. Is there a way to do this?
test='NAI\site_summaries\410_-_407_Central'
head,tail=os.path.split(test)
print(tail)
The code above returns 'site_summaries_-_407_Central', where it should be returning '410_-_407_Central'. Please keep in mind that I have a variable containing a list of these paths but I just chose to show one path for the sake of simplicity.

Python, string (consisting of variable and strings, concatenated) used as new variable name?

I've been searching on this but am coming up a little short on exactly how to do specifically what i am trying to do.. I want to concatentate a string (I guess it would be a string in this case as it has a variable and string) such as below, where I need to use a variable consisting of a string to call a listname that has an index (from another variable).. I simplified my code below to just show the relevant parts its part of a macro that is replacing values:
toreplacetype = 'type'
toreplace_indx = 5
replacement_string = 'list'+toreplacetype[toreplace_indx]
so... I am trying to make the string on the last line equal to the actual variable name:
replacement_string = listtype[5]
Any advice on how to do this is appreciated
EDIT:
To explain further, this is for a macro that is sort of a template system where I am indicating things in a python script that I want to replace with specific values so I am using regex to do this. So, when I match something, I want to be able to replace it from a specific value within a list, but, for example, in the template I have {{type}}, so I extract this, but then I need to manipulate it as above so that I can use the extracted value "type" to call a specific value from within a list (such as from a list called "listtype") (there is more than 1 list so I need to find the one called "listtype" so I just want to concatenate as above to get this, based on the value I extracted using regex
This is not recommended. Use a dict instead.
vars['list%s' % toreplacetype][5] = ...
Hrm...
globals()['list%s'% toreplacetype][toreplace_indx]
replacement_string = 'list'+toreplacetype+'['+str(toreplace_indx)+']'
will yield listtype[5] when you print it.
You need to basically break it into 5 parts: 1 string variable, 3 strings and an int casted to a string.
I think this is what you are asking?

Categories

Resources