I try to convert a string value into Dictionary format. I must respect this syntax but when I try to convert the string value into a dictionary I have a error. The json.load convert the bracket nested into a string and not into a dictionary format.
Do you have any idea to resolve this ?
import json
dict_info = "{\"strategy\" : \"" + "ok" + "\", \"aa\" : \"" + "{\"strategy\" : \"" +
"strg" + "\"}" + "\"}"
json.loads(dict_info)
Thank you
This is because here you are specifying that the value for key aa is a string by adding " before and after the value,
ie this part: \" " + "{\"strategy\" : \"" + "strg" + "\"}" + " \"
If you remove those the code should work, here
Code:
import json
dict_info = "{\"strategy\" : \"" + "ok" + "\", \"aa\" : " + "{\"strategy\" : \"" + "strg" + "\"}" + "}"
d = json.loads(dict_info)
print(d)
print(d['aa'])
Output:
{'strategy': 'ok', 'aa': {'strategy': 'strg'}}
{'strategy': 'strg'}
Related
I would like to collect different type of datas into a file. Here is a part of the code.
val = str(float(data[-1]))
val_dB = float(val)
val_dB = math.log(val_dB, 10) * 10
myfile = open('../../../MLI_values/mli_value.txt', 'a')
myfile.write(date_ID + " " + val + val_dB + "\n")
myfile.close()
But it gives back an error:
myfile.write(date_ID + " " + val + val_dB + "\n")
TypeError: cannot concatenate 'str' and 'float' objects
How can I solve it to put them together? (into columns) into a file?
Change:
myfile.write(date_ID + " " + val + val_dB + "\n")
to:
myfile.write(date_ID + " " + val + " " + str(val_dB) + "\n")
Given these 2 different string interpolation stanzas, neither one works. Both return the {n} and %(text) inserts as plain, raw text in the output. What am I missing?
Ive been using the %(string)s method forever in python2, now porting to python 3.
bulk_string = (
"bulk insert SomeDB.dbo.%(lru)s\n" +
"from 'C:\\Someplace\\"
"project\\%(filename)s'\n" +
"with (\n" +
" FIELDTERMINATOR = ',',\n" +
" ROWTERMINATOR = '%(term)s'\n" +
");"
% {
'lru': lru,
'filename': filename,
'term' : "\n"
}
)
and:
bulk_string = (
"bulk insert SomeDB.dbo.{0}\n" +
"from 'C:\\Someplace\\"
"project\\{1}'\n" +
"with (\n" +
" FIELDTERMINATOR = ',',\n" +
" ROWTERMINATOR = '{2}'\n" +
");"
.format(lru, filename, "\n")
)
either format or % apply only to the last string of your added strings. You could use """ (triple quoted strings) or parenthesize the strings (that, you did, but incorrectly):
bulk_string = (
"bulk insert SomeDB.dbo.{0}\n" +
"from 'C:\\Someplace\\"
"project\\{1}'\n" +
"with (\n" +
" FIELDTERMINATOR = ',',\n" +
" ROWTERMINATOR = '{2}'\n" +
");")
.format(lru, filename, "\\n")
or with triple quotes/raw string/automatic format positionning:
bulk_string = r"""bulk insert SomeDB.dbo.{}
from 'C:\Someplace\project\{}'
with (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '{}'
);""".format(lru, filename, "\\n")
Aside: the third parameter of format should be \\n or r\n if you want to generate a literal \n in your code.
Here is the most readable way to do this:
In Python 3, you can use literal string interpolation, or f-strings, see PEP 498 - just be careful with escaping the backslashes properly, especially in front of a curly bracket such as in C:\Someplace\project\\{filename}
lru = "myTable"
filename = "myFile"
rt = "\\n"
bulk_string = f"""bulk insert SomeDB.dbo.{lru}
from 'C:\Someplace\project\\{filename}'
with ( FIELDTERMINATOR = ,
ROWTERMINATOR = '{rt}');"""
I built a relation data model in Oracle and now creating a GUI using Python. I need a SQL statement to execute in my IDE but get a cx_Oracle.DatabaseError: ORA-00936: missing expression error message. This is a CTE that runs fine in TOAD and when I remove the CTE and put in a simple SQl statement it executes fine.
I can build a view in my DB and do a select * from but I don't want to go that way.
I'm new to Python so I'm sure there is a better way to do this.
import cx_Oracle
con = cx_Oracle.connect('Example', 'Example', "Example")
cur = con.cursor()
statement = ("with r1 as (" +
" select " +
" r.PARENT_ITEM_id, " +
" D.SC_ID, " +
" --F.TONS" +
" SUM(ROUND(F.TONS*Pic_Distro*2000*s.stk_lvl_mult)) as Stocking_Lvl" +
" from PIC_DISTRO_TBL D" +
" Left Join Part_Velocity_TBL P on (P.item_ID = D.Item_ID and D.SC_ID = P.SC_ID)" +
" Left Join Forecast_TBL F on (D.Bucket_ID = F.Bucket_ID and D.SC_ID =F.SC_ID)" +
" left join Stock_lvl_tbl S on (S.Velocity_id = P.VELOCITY_ID)" +
" left join item_tbl I on (i.item_ID = D.ITEM_ID)" +
" left join parent_item_tbl R on (r.PARENT_ITEM_id = i.PARENT_ITEM_id)" +
" Where F.MTH = '4'" +
" and F.YEAR = '2017'" +
" and P.Velocity_id in ('A','B','C')" +
" and D.SC_ID in ('01','02')" +
" -- and SUM(ROUND(F.TONS*Pic_Distro*2000*s.stk_lvl_mult)) > 0" +
" Group by " +
" r.PARENT_ITEM_id, D.SC_ID " +
" Order by " +
" D.SC_ID DESC, Stocking_lvl DESC" +
")," +
"R2 as (" +
"select r.Parent_Item_ID, o.SC_ID, " +
"coalesce(sum(avail_wt), 0) as Avail_Wt" +
" from" +
" open_inv_tbl O" +
" left join item_tbl I on (i.item_ID = o.ITEM_ID)" +
" left join parent_item_tbl R on (r.PARENT_ITEM_id = i.PARENT_ITEM_id)" +
" Where r.Parent_item_ID is not null" +
" Group by r.Parent_Item_ID,o.SC_ID)" +
"select " +
" r1.PARENT_ITEM_id, " +
" R1.SC_ID, R1.Stocking_Lvl , " +
" coalesce(R2.Avail_wt, 0 ) as Avail_Wt, " +
" coalesce(R2.Avail_wt/R1.Stocking_Lvl, 0) as Precantage" +
" From R1" +
" left join R2 on (R1.parent_item_id = R2.parent_item_id and R1.Sc_ID = R2.Sc_ID) " +
" Where R1.Stocking_lvl > '0' " +
" Order by SC_id Desc, Stocking_Lvl Desc)" )
cur.arraysize = 2000
cur.execute(statement)
Python offers you multi-line strings, when wrapped in triple quotes.
Try a execute a single string and review your query being correct.
statement = """
with r1 as (
select
r.PARENT_ITEM_id,
D.SC_ID,
--F.TONS
...
"""
cur.execute(statement)
I have a code for convert Jmeter JTL FILE TO CSV, but when I run the code, I have the following error: IndexError: list index out of range in line 32
This is the code
import sys
import re
import datetime
import time
startTime = time.time()
cnt = 0
cnt2 = 0
failCnt = 0
reCompile = re.compile("\s([^\s]*?)=\"(.*?)\"")
delimiterCharacterOut = ","
def writeCSVLine(line):
x = reCompile.findall(line)
a = dict((row[0], row[1]) for row in x)
try:
a['ts1'] = str(int(int(a['ts'])/1000))
x = str(datetime.datetime.fromtimestamp(float(a['ts1'])))[0:19]
b = a['ts'] + ",\"" + x + "\"," + a['t'] + "," + a['lt'] + ",\"" + a['s'] + "\",\"" + a['lb'] + "\"," + a['rc'] + ",\"" + a['rm'] + "\",\"" + a['tn'] + "\",\"" + a['dt'] + "\"," + a['by'] + ",\"" + a['sc'] + "\"," + a['ec'] + ",\"" + a['ng'] + "\"," + a['na'] + ",\"" + a['hn'] + "\"," + a['in'] + "\n"
except:
return -1
o.write(b)
return 1
print "Splitting JTL file"
try:
runArgv = sys.argv #Save the command line
jtlInfile = str(sys.argv[1]) #Name of JTL input file
cvsOutfile = str(sys.argv[2]) # Name of CVS output file
reFilter = str(sys.argv[3]) # Filter the labels (lb) for the filter
except:
print "Error: Input format: <input file> <output file> <Filter by regular expression>"
raise
try:
f = open(jtlInfile, "r")
o = open(cvsOutfile, "w")
except:
raise
print "Filtering on regular expression : " + reFilter
cmpFilter = re.compile(reFilter)
# o.write("timestamp" + ",\""+ "datetime" + "\n")
o.write("timeStamp" + ",\"" + "datetime" + "\"," + "elapsed" + "," + "Latency" + ",\"" + "success" + "\",\"" + "label" + "\"," + "responseCode" + ",\"" + "responseMessage" + "\",\"" + "threadName"+ "\",\"" + "dataType" + "\"," + "bytes" + ",\"" + "SampleCount" + "\"," + "ErrorCount" + ",\"" + "grpThreads" + "\"," + "allThreads" + ",\"" + "Hostname" + "\"," + "IdleTime" + "\n")
for line in f:
try:
if cmpFilter.search(line):
returnVal = writeCSVLine(line)
if returnVal<0:
failCnt += 1
else:
cnt2 += 1
except:
print 'Error in line : ', cnt, line
raise
cnt += 1
endTime = time.time()
print "Time taken : ", str(endTime-startTime)
print "Lines processed : ", cnt
print "Lines that passed the filter : ", cnt2
print "Lines skipped (error?) : ", failCnt
f.close()
o.close()
Log de CMD
The base tutorial is in : http://balasoftwaretesting.blogspot.com/2012/03/converting-jmeter-jtl-file-to-csv-file.html?spref=bl
From the sys.argv docs, sys.argv is the list of command line arguments passed to a Python script.
Your command line log shows that you ran python JtltoCsv_Jmeter.py, which would result in an empty list for sys.argv. The tutorial provides a jtl file as an argument to JtltoCsv_Jmeter.py:
JtltoCsv_Jmeter.py C:\JtlToCsvConverter\input\sample.jtl
So it looks like maybe an error in copy/paste :)
Looking into the script, you need to pass 3 command line arguments:
Source JTL file
Target CSV file
Regular expression filter
So you need to execute the script like:
JtltoCsv_Jmeter.py example.jtl example.csv "(.*)"
Also there is an option to switch JMeter's results output format to CSV, in order to do so use one of the following steps:
Add jmeter.save.saveservice.output_format=csv line to user.properties file (lives under /bin folder of your JMeter installation)
Pass the property value via -J command line argument as:
jmeter -Jjmeter.save.saveservice.output_format=csv
See Apache JMeter Properties Customization Guide for more information on JMeter properties and ways of passing, setting and overriding them.
I am trying to build a string that needs to contain specific double and single quotation characters for executing a SQL expression.
I need my output to be formatted like this:
" "Full_Stree" = 'ALLENDALE RD' "
where the value of ALLENDALE RD will be a variable defined through a For Loop. In the following code sample, the variable tOS is what I am trying to pass into the query variable.
tOS = "ALLENDALE RD"
query = '" "Full_Stree" = ' + "'" + tOS + "' " + '"'
and when I print the value of query variable I get this output:
'" "Full_Stree" = \'ALLENDALE RD\' "'
The slashes are causing my query to fail. I also tried using a modulus operator to pass the value of the tOS variable, but get the same results:
where = '" "Full_Stree" = \'%s\' "' % (tOS)
print where
'" "Full_Stree" = \'ALLENDALE RD\' "'
How can I get my string concatenated into the correct format, leaving the slashes out of the expression?
What you are seeing is the repr of your string.
>>> s = '" "Full_Stree" = \'ALLENDALE RD\' "'
>>> s # without print console displays the repr
'" "Full_Stree" = \'ALLENDALE RD\' "'
>>> print s # with print the string itself is displayed
" "Full_Stree" = 'ALLENDALE RD' "
Your real problem is the extra quotes at the beginning and end of your where-clause.
This
query = '" "Full_Stree" = ' + "'" + tOS + "' " + '"'
should be
query = '"Full_Stree" = ' + "'" + tOS + "'"
It is more clearly written as
query = """"Full_Stree" = '%s'""" % tOS
The ArcGis docs recommend something more like this
dataset = '/path/to/featureclass/shapefile/or/table'
field = arcpy.AddFieldDelimiters(dataset, 'Full_Stree')
whereclause = "%s = '%s'" % (field, tOS)
arcpy.AddFieldDelimiters makes sure that the field name includes the proper quoting style for the dataset you are using (some use double-quotes and some use square brackets).
Somehow the way I already tried worked out:
where = '" "Full_Stree" = \'%s\' "' % (tOS)
print where
'" "Full_Stree" = \'ALLENDALE RD\' "'
Can't you just use triple quotes?
a=""" "Full_Street" = 'ALLENDALE RD' """
print a
"Full_Street" = 'ALLENDALE RD'