counter=cur.rowcount
if(counter !=0):
values =[]
x=0
for row in content:
dValue, dYearOf, dMonthOf, Department = row
sent = str(dValue)
x=x+1
q=0
NounNum = 0
for noun in Noun:
AdjectiveNum=0
if " "+noun+" " in sent:
for adjective in Adjective:
AdjectiveNum=AdjectiveNum+1
if " "+adjective+" " in dValue:
sup=[]
for support in Support:
if " "+support+" " in sent:
sup=sup+[support]
supp = '-'.join(map(str, sup))
values.append(x.to_bytes)
values.append(noun)
values.append(NA[NounNum])
values.append(adjective)
values.append(supp)
values.append(dMonthOf)
values.append(dYearOf)
values.append(Department[:2])
f_value= ','.join(map(str, values))
sql=("""NSERT INTO `text_mining`(`Count`, `Noun`, `Noun_Code`, `Support`, `Adjective`, `Month`, `Year`, `Division`) VALUES (%s)""")
print(sql,f_value)
cur.execute(sql,f_value)
how can I put the an array value in my insert query? i tried some ways but I am trying to make an string that will combine all of my variable into 1 including with my array. obviously i am new with this so please help
Related
I am trying to work out how to iterate over a list and print out each item with a print statement describing what element is. my project is to create a user management system and print out something similar to the image I have attached.
The output I am trying to produce
The output I am getting
My code:
records = 0
userFirst = ["John"]
userLast = ["Doe"]
autoUsername = ["Johndoe91"]
autoPassword = ["123456789"]
hiddenPassword = ["*****789"]
userRole = ["User"]
userDept = ["Administration"]
users = []
confidentialUserDetails = []
users.append(userFirst + userLast + userRole + userDept + autoUsername + autoPassword)
confidentialUserDetails.append(users)
for row in range(len(confidentialUserDetails)):
records += 1
print("-" * 25)
print("Record: ", records)
for col in range(len(confidentialUserDetails[row])):
print(confidentialUserDetails[row][col])
Any help would be greatly appreciated. :)
Your data structures are unusual. I'm assuming that those lists are going to be provided to your code somehow and will, in practice, have multiple user details appended to them so that they are all the same length.
Anyhow, you can achieve the output you're looking for with some readable f-strings like this:
from functools import reduce
userFirst = ["John"]
userLast = ["Doe"]
autoUsername = ["Johndoe91"]
autoPassword = ["123456789"]
hiddenPassword = ["*****789"]
userRole = ["User"]
userDept = ["Administration"]
for row in range(len(userFirst)):
s = (f"""\
Name : {userFirst[row]} {userLast[row]}
Role : {userRole[row]}
Department : {userDept[row]}
Username : {autoUsername[row]}
Password : {hiddenPassword[row]}""")
maxlen = reduce(lambda x,y: max(x, len(y)), s.split("\n"), 0)
print(f"{s}\n{'-'*maxlen}\n")
Output:
Name : John Doe
Role : User
Department : Administration
Username : Johndoe91
Password : *****789
------------------------------
I created a dictionary called user instead of your list and after that I appended it to the second list and finally I printed the key and the value of the dictionary.
Also to get the full name I joined userFirst and userLast as string.
Code:
records = 0
userFirst = ["John"]
userLast = ["Doe"]
autoUsername = ["Johndoe91"]
autoPassword = ["123456789"]
hiddenPassword = ["*****789"]
userRole = ["User"]
userDept = ["Administration"]
confidentialUserDetails = [] # 2d list for asterisked passwords
users={'Name' : [' '.join(userFirst + userLast)] ,'Role' : userRole , 'Departement' : userDept ,'Username' : autoUsername ,'Password' : hiddenPassword }
confidentialUserDetails.append(users)
for user in confidentialUserDetails:
records += 1
print("-" * 25)
print("Record: ", records)
for ele,v in user.items():
print(ele,':',v[0])
Output:
-------------------------
Record: 1
Name : John Doe
Role : User
Departement : Administration
Username : Johndoe91
Password : *****789
Using a dictionary or f strings like the two other answers suggested is probably the best. But if you just want to use your current code to print your desired output, you can simply grab each item by its index number in your print statement.
Change the line:
print(confidentialUserDetails[row][col])
To something like this:
print("Name : ", confidentialUserDetails[row][col][0], confidentialUserDetails[row][col][1])
print("Role: : ", confidentialUserDetails[row][col][2])
Output:
-------------------------
Record: 1
Name : John Doe
Role: : User
Let me explain my problem I am looking to make a program that compares user absences in 2 different tools. The result of their absence is returned in 2 Excel file or in which I make a sum of each absence for each user thanks to a dictionary in python then I compare the 2 dictionary in order to find an error and suddenly the program returns the name of the user for whom the number of absences is not equal. And so I would like to know how to make my program send an email to the user concerned.
Sum of absences :
for row in range(1,253):
id2.append(feuille_2.cell_value(row, 2))
absence2.append(float(feuille_2.cell_value(row, 9)))
result = {}
for name in set(id2):
result[name] = 0
for i in range(len(id2)):
hours = float(absence2[i])
name = id2[i]
result[name] += hours
for name, hours in result.items():
print(name + ":\t" + str(hours))
id4 = [id1]
absence = []
for row in range(1,361):
absence.append(feuille_1.cell_value(row, 10))
id4.append(id1)
print(absence)
result2 = {}
for name2 in set(id4):
result2[name2] = 0
for i in range(len(id4)):
hours2 = absence[i]
name2 = id4[i]
result2[name2] += hours2
print(result2)
Comparaison of two dictionaries :
print("Seulement sur Sugar:", ", ".join(set(result).difference(result2)))
print("Seulement sur Chrnos:", ", ".join(set(result2).difference(result)))
for key in set(result).intersection(result2):
if result[key]!=result2[key]:
print("%s n'a pas declarer ses congée"% (key))
And I want help i want a function who send an email to each user concerned. After the comparaison
Having isues figuring out why this particular setup isnt working.
class market(object):
def __init__(self, market, coin):
self.coin = coin
self.market = market
req = requests.get(f"http://bittrex.com/api/v1.1/public/getticker?market={market}-{coin}")
sum = requests.get(f"https://bittrex.com/api/v1.1/public/getmarketsummary?market={market}-{coin}")
self.address = req.json()
self.marketsum = sum.json()
def ticker(self):
while True:
print(self.address["result"])
time.sleep(5)
def marketsummary(self):
print(f"Market Summary for {coin}")
print('_' * 20)
print("Market Name: ", self.marketsum['result']['MarketName'])
print("High: ", self.marketsum['result']['High']))
print("Low: ", self.marketsum['result']['Low'])
print("Volume: ", self.marketsum['result']['Volume'])
print("Last: ", self.marketsum['result']['Last'])
print("BaseVolume: ", self.marketsum['result']['BaseVolume'])
print("TimeStamp: ", self.marketsum['result']['TimeStamp'])
print("Bid: ", self.marketsum['result']['Bid'])
print("Ask: ", self.marketsum['result']['Ask'])
print("OpenBuyOrders: ", self.marketsum['result']['OpenBuyOrders'])
print("OpenSellOrders: ", self.marketsum['result']['OpenSellOrders'])
print("Previous Day: ", self.marketsum['result']['PrevDay'])
print("Created: ", self.marketsum['result']['Created'])
print("DisplayMarketName: ", self.marketsum['result']['DisplayMarketName'])`
Ive previously used this method with static(?jaron?) variables in if statements, such as
usdt_ticker = requests.get("https://bittrex.com/api/v1.1/public/getticker?market=USDT-ADA")
btc_ticker = requests.get("https://bittrex.com/api/v1.1/public/getticker?market=BTC-ADA")
eth_ticker = requests.get("https://bittrex.com/api/v1.1/public/getticker?market=ETH-ADA")
print("Which trade pairing would you like for this coin?")
tradepair = input("> ")
if str.lower(tradepair) == "usdt" or "tether":
actual_ticker = usdt_ticker.json()
elif str.lower(tradepair) == "btc" or "bitcoin":
actual_ticker = btc_ticker.json()
elif str.lower(tradepair) == "eth" or "ethereum":
actual_ticker = eth_ticker.json()
else:
print("Sorry that trading pair isnt currently being monitored by this system")
print("Now viewing Cardano /", str.upper(tradepair), " trading." )
current_price = actual_ticker["result"]["Last"]
but with the self.marketsum['result']['MarketName'] its not working. If theres any input as to why this is happening and how to fix it I would be greatly appreciative. The error I am getting is
TypeError: list indicies must be integers or slices, not str
From the developer's guide, the json structure of a response from /public/getmarketsummary looks like:
{
"success" : true,
"message" : "",
"result" : [{
"MarketName" : "BTC-LTC",
"High" : 0.01350000,
"Low" : 0.01200000,
"Volume" : 3833.97619253,
"Last" : 0.01349998,
"BaseVolume" : 47.03987026,
"TimeStamp" : "2014-07-09T07:22:16.72",
"Bid" : 0.01271001,
"Ask" : 0.01291100,
"OpenBuyOrders" : 45,
"OpenSellOrders" : 45,
"PrevDay" : 0.01229501,
"Created" : "2014-02-13T00:00:00",
"DisplayMarketName" : null
}
]
}
Notice the result is actually a list containing a single element. They don't state why it is in a list and I can't get it to return a list with more than one element.
For now, it should be fine to change the lines accessing marketsum from
self.marketsum['result']['last']
to
self.marketsum['result'][0]['last']
Probably also add a check that the list is not empty.
Hi I am trying to insert a python list into a single column but it keeps giving an error on the syntax.
New to this. Appreciate any help. Thanks.
from time import time
import MySQLdb
import urllib
import re
from bs4 import BeautifulSoup
db = MySQLdb.connect("localhost","testuser","test123","testdb" )
cursor = db.cursor()
x=1
while x<2:
url = "http://search.insing.com/ts/food-drink/bars-pubs/bars-pubs?page=" +str(x)
htmlfile = urllib.urlopen(url)
soup = BeautifulSoup(htmlfile)
reshtml = [h3.a for h3 in soup.find("div", "results").find_all("h3")]
reslist = []
for item in reshtml:
res = item.text.encode('ascii', 'ignore')
reslist.append(' '.join(res.split()))
sql = "INSERT INTO insing(name) \
VALUES %r" \
% reslist
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
db.close()
x += 1
The output for SQL is
'INSERT INTO insing(name) VALUES [\'AdstraGold Microbrewery & Bistro Bar\', \'Alkaff Mansion Ristorante\', \'Parco Caffe\', \'The Fat Cat Bistro\', \'Gravity Bar\', \'The Wine Company (Evans Road)\', \'Serenity Spanish Bar & Restaurant (VivoCity)\', \'The New Harbour Cafe & Bar\', \'Indian Times\', \'Sunset Bay Beach Bar\', \'Friends # Jelita\', \'Talk Cock Sing Song # Thomson\', \'En Japanese Dining Bar (UE Square)\', \'Magma German Wine Bistro\', "Tam Kah Shark\'s Fin", \'Senso Ristorante & Bar\', \'Hard Rock Cafe (HPL House)\', \'St. James Power Station\', \'The St. James\', \'Brotzeit German Bier Bar & Restaurant (Vivocity)\']'
what about
insert into table(name) values ('name1'), ('name2'), ... , ('name36');
Inserting multiple rows in a single SQL query?
That might help too.
EDIT
I automated the process as well:
dataSQL = "INSERT INTO PropertyRow (SWID, Address, APN, PropertyType, PermissableUse, UseDetail, ReviewResult, Analysis, DocReviewed, AqDate, ValuePurchase, ValueCurrent, ValueDate, ValueBasis, ValueSale, SaleDate, PropPurpose, LotSize, Zoning, ParcelValue, EstRevenue, ReqRevenue, EnvHistory, TransitPotential, PlanObjective, PrevHistory, LastUpdDate, LastUpdUser)"
fields = "VALUES ("+"'"+str(rawID)+"', "
if(cell.ctype != 0):
while column < 27:
#column 16 will always be blank
if (column == 16):
column += 1
#column 26 is the end
if (column == 26):
fields += "'"+str(sh.cell_value(rowx=currentRow, colx=column)) + "'"
else:
#append to the value string
fields += "'"+str(sh.cell_value(rowx=currentRow, colx=column)) + "', "
#print fields
column+=1
fields += ');'
writeFyle.write(dataSQL)
writeFyle.write(fields)
In this implementation I am writing an insert statement for each row that I wanted to insert. This wasn't necessary but it was much easier.
Objective:
Extract String data, Currency value , [type of currency] and date.
Content of file:
[["1234567890","Your previous month subscription point is <RS|$|QR|#> 5,200.33.Your current month month subscription point is <RS|$|QR|#> 1,15,200.33, Last Year total point earned <RS|$|QR|#> 5589965.26 and point lost in game is <RS|$|QR|#> 11520 your this year subscription will expire on 19-04-2013. 9. Back"],["1234567890","Your previous month subscription point is <RS|$|QR|#> 5,200.33.Your current month month subscription point is <RS|$|QR|#> 1,15,200.33, Last Year total point earned <RS|$|QR|#> 5589965.26 and point lost in game is <RS|$|QR|#> 11520 your this year subscription will expire on 19-04-2013. 9. Back"]]
What I have done so far:
def read_file():
fp = open('D:\\ReadData2.txt', 'rb')
content = fp.read()
data = eval(content)
l1 = ["%s" % x[1] for x in data]
return l1
def check_currency(l2):
import re
for i in range(l2.__len__()):
newstr2 = l2[i]
val_currency = []
val_currency.extend(re.findall(r'([+-]?\d+(?:\,\d+)*?\d+(?:\.\d+)?)',newstr2))
print " List %s " % val_currency
for i in range(len(val_currency)):
val2 = val_currency[i]
remove_commas = re.compile(r',(?=\d+)*?')
val3 = remove_commas.sub('', val2)
print val3
if __name__=="__main__":main()
EDIT UDP
I am able to extract the currency value but with the currency of -ve value are conflicting with date format(dd-mm-yyyy). And during extracting string value its also extracting [.|,|] how not to read these characters.
Ouput of check_currency:
>List ['5,200.33', '1,15,200.33', '5589965.26', '11520', '19', '-04', '-2013']
>5200.33
>115200.33
>5589965.26
>11520
>19
>-04
>-2013
Expected Ouput of check_currency:
>List ['5,200.33', '1,15,200.33', '5589965.26', '11520']
>5200.33
>115200.33
>5589965.26
>11520
I added this <RS|$|QR|#>\s* at the first part of your regular expression so as
to be used as prefix for the currency value you want to match.
You can change your code to this one:
def check_currency(l2):
import re
for i in range(l2.__len__()):
newstr2 = l2[i]
val_currency = []
val_currency.extend(re.findall(r'<RS|$|QR|#>\s*([+-]?\d+(?:\,\d+)*?\d+(?:\.\d+)?)',newstr2))
# skip empty strings and remove comma characters
val_currency = [v.replace(',', '') for v in val_currency if v]
print " List %s " % val_currency$
for i in range(len(val_currency)):
val2 = val_currency[i]
remove_commas = re.compile(r',(?=\d+)*?')
val3 = remove_commas.sub('', val2)
print val3
Output:
List ['5200.33', '115200.33', '5589965.26', '11520']
5200.33
115200.33
5589965.26
11520
aditions in the code:
val_currency.extend(re.findall(r'<RS|$|QR|#>\s*([+-]?\d+(?:\,\d+)*?\d+(?:\.\d+)?)',newstr2))
val_currency = [v.replace(',', '') for v in val_currency if v]