Formatting SQL rows to a list in python - python

I have a bunch of SQL rows that I have converted to lists:
cursor = conn.cursor()
result = cursor.execute(SQLqueries.sql_qry1)
for row in result:
row_to_list = list(row)
print(row_to_list)
The output of this is lists like:
['FreqBand,Frequency,0, 5, 10\r\n1,0.006,16.56,25.15,30.96\r\n']
['FreqBand,Frequency,0, 5, 10\r\n1,0.006,12.56,15.27,31.90\r\n']
['FreqBand,Frequency,0, 5, 10\r\n1,0.006,16.36,25.15,34.46\r\n']
I would like to edit these lists to exclude the first two words and replace the "\r\n" characters with commas. I've tried this to get rid of the 'FreqBand,Frequency':
for row in result:
row_to_list = list(row)
i = 0
for each in row_to_list:
row_to_list[i].replace('FreqBand', '')
i += 1
print(row_to_list)
but the output of this seems to get rid of half the first list and doesn't edit any of the others. Any help on this would be appreciated.

You need to assign the result of replace() back to the list element. And use range() to get a sequence of numbers instead of incrementing i yourself.
for row in result:
row_to_list = list(row)
for i in range(len(row_to_list)):
row_to_list[i] = row_to_list[i].replace('FreqBand,Frequency,', '').replace('\r\n', ',')
print(row_to_list)

First of all, the list below has a size of 1 rather than 8-10 that I assumed when I first saw the question. So initially, please check if that is something you are aware of.
['FreqBand,Frequency,0, 5, 10\r\n1,0.006,16.56,25.15,30.96\r\n']
In this way, when you iterate over this list with for each in row_to_list:, all you will get is the string that has no difference from the string you would get with row_to_list[0].
Secondly, you might want to double check what you are trying to accomplish with the counter i. In the case of manipulating first elements of each list you name as row_to_list, all you need to do is to access by index then reassign the variable.
for row in result:
row_to_list = list(row)
row_to_list[0] = row_to_list[0].replace('FreqBand,Frequency,', '')
row_to_list[-1] = row_to_list[-1].replace('\r\n', ',')

Related

How to arrange arrays in a list?

I have a list of personal data(id_code,birth_year,born_in) and i want to sort the any arrays in list but i have a problem in this work.
my list data :
data = [
'id_code:3211238576;birth_year:1350;born_in:Boushehr',
'id_code:9801233575;born_in:Argentina;birth_year:1360',
'born_in:Portugal;id_code:0219206431;birth_year:1358',
'id_code:0021678913;born_in:Shiraz;birth_year:1120',
'id_code:1101102135;born_in:Gilan;birth_year:1152',
]
The code I wrote and has an bug:
for i in data:
s = ''.join(sorted(i))
print(s)
my code output:
01112233355678:::;;B___abbcddeeehhhiiinnooorrrrstuy
00112333556789:::;;A___aabbcddeeeghiiiinnnnoorrrrtty
00111223345689:::;;P___aabbcddeeghiiilnnooorrrrttuy
00011112236789:::;;S___aabbcddeehhiiiinnoorrrrtyz
00111111122355:::;;G___aabbcddeehiiiilnnnoorrrty
But! The code to i want to have in output(True answer):
id_code:3211238576,born_in:Boushehr,birth_year:1350
id_code:9801233575,born_in:Argentina,birth_year:1360
id_code:0219206431,born_in:Portugal,birth_year:1358
id_code:0021678913,born_in:Shiraz,birth_year:1120
id_code:1101102135,born_in:Gilan,birth_year:1152
Please help me to solve this problem
Assuming you want your fields to be in specific order, try this one: (I put comments in code for clarification):
data = [
'id_code:3211238576;birth_year:1350;born_in:Boushehr',
'id_code:9801233575;born_in:Argentina;birth_year:1360',
'born_in:Portugal;id_code:0219206431;birth_year:1358',
'id_code:0021678913;born_in:Shiraz;birth_year:1120',
'id_code:1101102135;born_in:Gilan;birth_year:1152',
]
def sorter(x: str):
# getting the field name
field = x.split(':')[0]
# returning it's index from "sorted_by" list
return sorted_by.index(field)
# The index of these fields will be used for sorting in "sorter" function.
sorted_by = ['id_code', 'born_in', 'birth_year']
result = []
for item in data:
# splitting the fields
splited = item.split(';')
splited.sort(key=sorter)
# building the line back and append it
result.append(';'.join(splited))
for i in result:
print(i)
output :
id_code:3211238576;born_in:Boushehr;birth_year:1350
id_code:9801233575;born_in:Argentina;birth_year:1360
id_code:0219206431;born_in:Portugal;birth_year:1358
id_code:0021678913;born_in:Shiraz;birth_year:1120
id_code:1101102135;born_in:Gilan;birth_year:1152
Now you can easily change the fields order in sorted_by list and see the result.
Try
out = [';'.join(reversed(sorted(x.split(';')))) for x in data]
print(out)
This takes every element of the data list and splits it in three strings, each of which contains one of the three attributes. Then, it arranges the three strings in reversed alphabetical order and joins them back into one string, separated by ';'

Python sqllite3 doesn't return list

I have a list of values (only one value for now)
testList = ["value1"] #or is this an array in python??
and I would like to get this list from database
so I tried like this (assuming I have myTerribleTable in my DB and it has one atribute named "myAtribute" which has values "value1"
conn = sqlite3.connect("mydb.db")
so I execute the query: print(conn.execute(SELECT myAtribute FROM myTerribleTable).fetchall())
so I expect to get back ['value1']
but I get [('value1',)] this is not a list, it looks like a tuple even prepending evething with list like that print(list(conn.execute(SELECT myAtribute FROM myTerribleTable).fetchall())) doesn't work I still get [('value1',)] instead of desired ['value1']
not sure what is happening
Thanks for Anwsering and Best Regards
rows = conn.execute("SELECT myAttribute FROM myTerribleTable").fetchall()
output = List()
for row in rows:
output.append(row.["myAttribute"])
With this loop you could convert the row objects into a list of values
print(list(conn.execute(SELECT myAtribute FROM myTerribleTable).fetchall()[0]))
How about this?

Result of my CSV file generated contains comma's, brackets

I am generating a csv which contains the results I expect, all the numbers are right.
However, presentation wise it contains parentheses around everything and comma's etc.
Is there a way I can remove these?
I tried adding comma as a delimiter but that didn't solve it.
Example output:
Sure: ('Egg',)
results = []
results1 = []
results2 = []
results3 = []
results4 = []
results5 = []
results6 = []
cur.execute(dbQuery)
results.extend(cur.fetchall())
cur.execute(dbQuery1)
results1.extend(cur.fetchall())
cur.execute(dbQuery2)
results2.extend(cur.fetchall())
cur.execute(dbQuery3)
results3.extend(cur.fetchall())
cur.execute(dbQuery4)
results4.extend(cur.fetchall())
cur.execute(dbQuery5)
results5.extend(cur.fetchall())
cur.execute(dbQuery6)
results6.extend(cur.fetchall())
with open("Stats.csv", "wb") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Query1', 'Query2', 'Query3', 'Query4', 'Query5', 'Query6', 'Query7'])
csv_writer.writerows(zip(*[results, results1, results2, results3, results4, results5, results6]))
The zip function is returning a list of tuples [(x, y), (t, s)...]
The writerows method expects a list of lists. So, I think you should format the zip return before call the writerows. Something like that should work:
result = zip(results, results1, results2, results3, results4, results5, results6)
csv_writer.writerows([list(row) for row in result])
EDIT:
I think I understood the problem you are having here (so ignore my previous answer above).
The fetchall function is returning a list of tuples like [(x,), (y,)]
So, then your resultsX variables will have this format. Then, you are applying a zip between these lists (see here what zip does).
If for example we have
results = [(x,), (y,)]
results1 = [(t,), (z,)]
When you run the zip(results, results1), it will return:
[((x,), (t,)), ((y,), (z,))]
So, that's the format of the list you are passing to the writerows, which means the first row will be: ((x,), (t,)) where the element one is: (x,) and the second one is (t,)
So, not sure what you are expecting to write in the CSV with the zip function. But the result you are getting is because your elements to write in the csv are tuples instead of values.
I don't know the query you are doing here, but if you are expecting just one field per each result, maybe then you need to strip out the tuple in each resultsX variable. You can take a look how to do it in this thread: https://stackoverflow.com/a/12867429/1130381
I hope it helps, but that's the best I can do with the info you provided.

How to determine row index using a single print statement?

Given the data for the row index to be found as max_sw and list is sw_col.
I tried this and some other variation, but nothing worked.
print(i for i in range(len(sw_col)) if sw_col[i]== max_sw)
The line you have is almost there. If you put the generator into a list and use only index position zero, this will give you the correct answer:
sw_col = ['a','b','c']
max_sw = 'c'
print([i for i in range(len(sw_col)) if sw_col[i]== max_sw][0]) # prints 2
A more concise solution would be to look up the item directly in the list, like so:
sw_col = ['a','b','c']
max_sw = 'c'
print(sw_col.index(max_sw)) # prints 2

inserting into a list but ensuring that there isnt multiple insertions in the list

Ive been trying to create a part of code that takes data from an excel file then adds it into a list but only once. all other times should be ignored, ive managed to get all the data i need, just need to know how to pop unwanted duplicates. Also wondering if i should do this in a dictionary and how it would be done if i did
for cellObj in rows:<br>
Lat = str(cellObj[5].value)<br>
if 'S' in Lat:<br>
majorCity.append(str(cellObj[3].value))<br>
print(majorCity)<br>
elif majorCity == majorCity:<br>
majorCity.pop(str(cellObj[3].value))<br>
You can use set(), it will remove duplicates from a sequence.
a= set()
a.add("1")
a.add("1")
print a
Output:
set(['1'])
set is indeed a good way to do this:
>>> my_list = [1,1,2,2]
>>> my_list_no_dups = list(set(my_list))
>>> my_list_no_dups
[1, 2]
but it will not necessarily preserve the order of the list. If you do care about the order, you can do it like this:
my_list_no_dups = []
for item in my_list:
if item not in my_list_no_dups:
my_list_no_dups.append(item)

Categories