I have a list of rows imported from SQL (using PyMySQL), based on user inputs. Sometimes these returns can number over 5000 rows (these are stored in a 2D array, with at least 4 columns). This is not practical for a user to be able to read when printed out on the screen.
I have implemented a limiter, which will return the first X number of rows, based on a user's input. However, I would like to return a sample of the rows, instead of the first X.
i.e. if a user selected 100 rows, instead of getting the first 100 rows, they would get a sample of size 100, made up of random rows in the array. Is there a way to do this?
The code I have so far is:
with conn:
cursor = conn.cursor()
cursor.execute(query, Pop_Lim)
city = cursor.fetchall()
if len(city) >= 50:
print()
print("This search will return ",len(city), "rows of data.")
Ret_Lim = int(input("How many rows of data do you want to display? "))
print()
with conn:
cursor = conn.cursor()
cursor.execute(query, Pop_Lim)
city = cursor.fetchmany(Ret_Lim)
print("ID : CountryCode : District : Population")
for row in city:
print(row["ID"], row["Name"]," : ",row["CountryCode"]," : ",row["District"]," : ",row["Population"]) # insert spacers for legibility purposes
print()
print(Ret_Lim,"rows of data returned, as requested.")