[kwlug-disc] Any Python Experts?
John Driezen
jdriezen at sympatico.ca
Thu Sep 13 10:15:25 EDT 2018
I am trying to write a program to plot scrap copper prices using python,
sqlite3 and plotly. I have run into a bit of a snag. The plotly
library only works with Python 2.7, and I wrote a scraper to work with
Python 3.5. When I read the data from the sql database using Python
2.7.2 I get an unexpected 'u' in front of my date values. Using the
Python 3.5.2 interpreter produces the correct results.
john at LinuxMint18 ~/scrap_prices_scraper $ cat read_data.py
import sqlite3
import os
DB_FILE = '/home/john/scrap_prices_scraper/scrapmetalprices.db'
# check for existing database file
if os.path.isfile(DB_FILE):
connection = sqlite3.connect(DB_FILE)
cursor = connection.execute ("SELECT * from PRICES WHERE
MATERIAL='#1 Bright Copper'")
price=[]
dates=[]
for row in cursor:
dates.append(row[4]) # store date in list
price.append(row[2]) # store price in list
print (dates, price)
connection.close
else:
print("Database does not exist.")
raise SystemExit
john at LinuxMint18 ~/scrap_prices_scraper $ python --version
Python 2.7.12
john at LinuxMint18 ~/scrap_prices_scraper $ python3 --version
Python 3.5.2
The incorrect results with the Python 2.7 interpreter:
john at LinuxMint18 ~/scrap_prices_scraper $ python read_data.py
([u'2018-08-31'], [3.13])
([u'2018-08-31', u'2018-09-04'], [3.13, 3.03])
([u'2018-08-31', u'2018-09-04', u'2018-09-10'], [3.13, 3.03, 3.03])
([u'2018-08-31', u'2018-09-04', u'2018-09-10', u'2018-09-12'], [3.13,
3.03, 3.03, 3.03])
The correct results with the Python3 interpreter:
john at LinuxMint18 ~/scrap_prices_scraper $ python3 read_data.py
['2018-08-31'] [3.13]
['2018-08-31', '2018-09-04'] [3.13, 3.03]
['2018-08-31', '2018-09-04', '2018-09-10'] [3.13, 3.03, 3.03]
['2018-08-31', '2018-09-04', '2018-09-10', '2018-09-12'] [3.13, 3.03,
3.03, 3.03]
john at LinuxMint18 ~/scrap_prices_scraper $ exit
exit
Script done on Thu 13 Sep 2018 09:57:20 AM EDT
Does anyone have any suggestions?
John Driezen
More information about the kwlug-disc
mailing list