Create correct loop output with python function [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Could someone help me with following function:
def nagios_chart():
alpha = [chr(item).upper() for item in range(ord('b'), ord('l')+1)]
for idx, column in enumerate(alpha):
print worksheet.write_column('column + 1', nagios_data[idx])
if __name__ == '__main__':
nagios_chart()
I need output like this:
worksheet.write_column('B1', nagios_data[0])
worksheet.write_column('C1', nagios_data[1])
worksheet.write_column('D1', nagios_data[2])
worksheet.write_column('E1', nagios_data[3])
worksheet.write_column('F1', nagios_data[4])
worksheet.write_column('G1', nagios_data[5])
worksheet.write_column('H1', nagios_data[6])
worksheet.write_column('I1', nagios_data[7])
worksheet.write_column('J1', nagios_data[8])
worksheet.write_column('K1', nagios_data[9])
worksheet.write_column('L1', nagios_data[10])

To use the column as a variable, remove it from the quotes so it isn't treated as a string literal. Then you can concatenate it with '1'.
def nagios_chart():
alpha = [chr(item).upper() for item in range(ord('b'), ord('l')+1)]
for idx, column in enumerate(alpha):
print worksheet.write_column(column + '1', nagios_data[idx])

Related

python string find and look up on string until found space from json [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Below is my data.
'{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}'
My output should be as below
[{"a":5647953897,"b":"323299901059958183671030","c":1605858513465},{"a":5647953897,"b":"323299901059958183671030","c":1605858513465}]
This should solve your case.
from json import JSONDecoder, JSONDecodeError
import re
NOT_WHITESPACE = re.compile(r'[^\s]')
data = '''{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}'''
def decode_stacked(document, pos=0, decoder=JSONDecoder()):
while True:
match = NOT_WHITESPACE.search(document, pos)
if not match:
return
pos = match.start()
try:
obj, pos = decoder.raw_decode(document, pos)
except JSONDecodeError:
raise
yield obj
for obj in decode_stacked(data):
print(obj)

Create email with the firsts letters of the name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any magic python way to get the firsts letters of each name and join with '#company.com' to create a email address?
Here is how I did:
name = ['Elon Reeve Musk']
full_name = [word[0].lower() for word in name[0].split(' ')]
firts_letters = "".join(full_name)
username = '%s#company.com' %(firts_letters)
The result is erm#company.com
Here's some "python magic"
name = ['Elon Reeve Musk']
f"{''.join(filter(str.isupper, name[0]))}#company.com".lower()
>>> erm#company.com
Whether this is better than your method is debatable. Most of the time a few lines of easy to read code is better than a one line hack.
My recommendation would be
name = ['Elon Reeve Musk']
initials = ''.join(word[0] for word in name[0].split())
f'{initials.lower()}#company.com'
>>> erm#company.com

Very simple python cycle string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to write in a file which changes the name with the counter.
for j in range(30):
fileout.write("bla bla")
and fileout should be
file001 if j=1
file002 if j=2
...
...
I cannot format it in the right way.
Anyone can help me?
Thank you
You can try to use the .zfill( < number of digits > ) option. It will complet your string with 0.
For example, this code should work :
radical ='file'
numberOfDigits = 3
for j in range( 30 ) :
fileout = open ( radical + '%s' %(str(j).zfill(numberOfDigits)) + '.txt' , 'w' )
fileout.write('bla bla')
fileout.close()
Not sure if below means what you needed ?
Code below will create file000 - file029 with 'bla bla' inside
for i in range(30):
with open("file%03d" % i, 'w') as f:
f.write('bla bla')

Return outside function error - help needed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
def getPressAve(odbname):
odb=openOdb(odbname)
lastFrame=odb.steps['Step-1'].frames[-1]
pressure=lastFrame.fieldOutputs['CPRESS']
press=[[0,0]] # sets the first element to [0,0]
for n in pressure.values:
gridPt=part1.nodes.getFromLabel(n.nodeLabel)
coord=assemb.getCoordinates(gridPt)
press.append([n.nodeLabel,n.data,coord])
press=avePress=press[1:] # removes the first element
press.sort(Comp_X)
print ('pressure extracted')
index=0
while index<len(press):
sum=0
tally=0
if index!=0:
sum=sum+press[index-1][1]
tally=tally+1
if index!=1:
sum=sum+press[index-2][1]
tally=tally+1
if index!=2:
sum=sum+press[index][1]
tally=tally+1
if index<len(press)-1:
sum=sum+press[index+1][1]
tally=tally+1
if index<len(press)-2:
sum=sum+press[index+2][1]
tally=tally+1
average=sum/tally
avePress[index][1]=average
index=index+1
odb.close()
print ('pressure averaged')
return avePress
In Python, indentation matters. As is, you're defining a function called getPressAve which does only this:
odb=openOdb(odbname)
After you've defined your function, you go on to do
lastFrame=odb.steps['Step-1'].frames[-1]
and such outside of the function. That's not what you want. The solution is to indent everything after that odb=openOdb(odbname) line to that level, so those lines are interpreted as being part of the body of the function.
You forgot to properly indent your code:
def getPressAve(odbname):
odb=openOdb(odbname)
...
print ('pressure averaged')
return avePress
As yours has not been, the return keyword is featured outside of a function, and hence the error: SyntaxError: 'return' outside function.

Python GQL query not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
ID = int(data_key)
results = db.GqlQuery("SELECT * FROM PatientMeds WHERE patientinfo_ID='ID'")
where i have done wrong...
Thanks..
Use a parameter (examples 1, 2):
ID = int(data_key)
results = db.GqlQuery("SELECT * FROM PatientMeds WHERE patientinfo_ID=:1", ID)
# or
results = db.GqlQuery("SELECT * FROM PatientMeds WHERE patientinfo_ID=:id", id=ID)
You could also use the more "Pythonic" model API:
from path.to.your.models import PatientMeds
results = PatientMeds.all().filter('patientinfo_ID =', int(data_key))

Categories