简体   繁体   中英

Django QuerySet values_list returning unknown characters

I am using django to pull some data from the MySQL server I have. Currently I have it pulling a whole column of data (the "light" column) from the SQL server.

To do this I am using the following code

weather = weatherdata.objects.values_list('light', flat=True)

lightdata = list(weather)

When I do this the lightdata list looks like this:

[35L, 53L, 77L, 99L, 49L, 46L, 28L, 13L, 2L, 0L, 0L, 0L]  

Those values are correct, its just there is an L at the end of each of these. How can I remove the L from this list?

You can do something like this:

l = [35L, 53L, 77L, 99L, 49L, 46L, 28L, 13L, 2L, 0L, 0L, 0L]
l = [int(item) for item in l]

Also if you want to delete the replicated items:

l = [35L, 53L, 77L, 99L, 49L, 46L, 28L, 13L, 2L, 0L, 0L, 0L]
l = list(set(l))
l = [int(item) for item in l]

The L at the end is indicative that it's a long type. It won't print through a print statement, and it's just telling you that the number stored here is definitely not an integer.

If you really wanted to get rid of them, then I suppose you could do something like the following:

lightdataNew = []
for num in lightdata:
    lightdataNew.append(int(num))
lightdata = lightdataNew[:] 

The L indicates a long integer.

>>> a = long(5)
>>> a
5L
>>> print a
5

You could convert the type of the elements in the list depending on your needs but it may not be necessary.

>>> b = [int(i) for i in list_of_longs]
>>> c = [str(i) for i in list_of_longs]

Edit: Seems Goin, Makoto and I were all typing answers at the same time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM