This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Why doesn't the main() function run when I start a Python script? Where does the script start running (what is its entry point)?
(5 answers)
Closed 6 months ago.
My code goes through the compiler but doesn't replace what needs to be replaced in the new file. All files are in the same directory and I've tried running it as ./06_1 but nothing changed. Here is the code. Any help would be appreciated.
def replace_and_chars(fname):
f = open(fname,"r", encoding='windows-1255')
file_buffer_contents = f.read()
file_buffer_contents.replace("&","\\x26")
fout = open(fname.replace(".xml","") + "replaced.xml","w+")
fout.write(file_buffer_contents)
fout.close
f.close()
past = replace_and_chars("06_1.xml")
Related
This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed last month.
//func_to_exec parameter is coming from database dynamically.
func_to_exec='split("\|")[0].split(",")[1]'
pl='mancity,manunited,arsenal|2|3|4|5'
is there anyway to call
pl.func_to_exec
I saw exec and eval functions are only for integers. I cant find any solution for strings.
Thx for suggestions.
You can use the exec function for that:
pl = 'mancity,manunited,arsenal|2|3|4|5'
func_to_exec = 'split("\|")[0].split(",")[1]'
exec(f'result = pl.{func_to_exec}')
print(result) # Output: 'manunited'
This question already has answers here:
Store output of subprocess.Popen call in a string [duplicate]
(15 answers)
Closed 3 years ago.
I have to run a shell command from python and get the output of that command into a python variable
python_var = subprocess.check_output('/opt/PPPP/QQQ/my_cmd -option1 -option2 /opt/XXXX/YYYY/ZZZZZ/my_file')
You need to hand-split the arguments into a sequence, not just pass a string (passing a whole string requires shell=True on Linux, but introduces all sorts of security/stability risks):
python_var = subprocess.check_output(['/opt/PPPP/QQQ/my_cmd', '-option1', '-option2', '/opt/XXXX/YYYY/ZZZZZ/my_file'])
This question already has answers here:
Can you add new statements to Python's syntax?
(13 answers)
Add new statements to Python without customizing the compiler
(2 answers)
Closed 4 years ago.
Can I create a statement in python 2.7?
Something similar to the print statement, that it's like a function, but gets the parameters without parenthesis.
This question already has answers here:
How to get only the last part of a path in Python?
(10 answers)
Closed 5 years ago.
Is it possible to manipulate a variable, for example:
file = "/Python/work.txt"
to just list work.txt, without /Python? excluding everything on the left of the "/"?
Thank you
Of course! Simply do this:
file = "/Python/work.txt"
excluded = file.split("/")[-1]
This would return "work.txt" in the excluded variable.
This question already has answers here:
What does return mean in Python? [closed]
(2 answers)
Why is "None" printed after my function's output?
(7 answers)
Python: Why "return" won´t print out all list elements in a simple for loop and "print" will do it?
(4 answers)
Closed 5 years ago.
I have been practising python and have a small question. I am working with DNA sequences and so I simply wanted to make a small function that just returned the record.ids.
from Bio import AlignIO
my_alignment = Align.IO.read("multipleseqfile.fa","fasta")
def get_id_names(alignment):
for record in alignment:
return record.id
print get_id_names(my_alignment)
I had done a for loop before that prints the names nicely but I wanted to improve my script and make these exercises into functions. However, when I use this function, it only returns the first record id (and there is a list of 30-40). I switched the return record.id to print record.id, and it does print all the names but then I get a None at the end of the output. Not sure what is going on here?