Please point out my mistake in the following program - python-2.7

Here m trying to extract a list of blacklist IP from a specific site and trying to make an excel sheet of the following fields :
IP , date ...... New updated code is :
import xlwt
import urllib
def Bl():
link = 'https://www.dshield.org/ipsascii.html?limit=100'
p = urllib.urlopen(link)
text = p.readlines()
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('python')
sheet.write(0,0,'Blacklist IP')
sheet.write(0,1,'Reports')
sheet.write(0,2,'abcd')
sheet.write(0,3,'date')
sheet.write(0,4,'Date')
row = 1 #counter for rows.
col = 0 #counter for columns.
x = 0 #counter for printing the n'th element in the string w.
for line in text:
li = line.strip()
w = line.split()
if not li.startswith("#"):
sheet.write(row,col,w[0])
sheet.write(row,1,w[1])
sheet.write(row,2,w[2])
sheet.write(row,3,w[3])
sheet.write(row,4,w[4])
row = row + 1
wbk.save('new.xls')
Bl()

I believe it should be this, though my python isn't great:
import xlwt
import urllib
link = 'https://www.dshield.org/ipsascii.html?limit=100'
p = urllib.urlopen(link)
text = p.readlines()
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('Blacklist IP')
row = 1 #counter for rows.
col = 0 #counter for columns.
#x = 0 #counter for printing the n'th element in the string w.
for line in text:
li = line.strip()
w = line.split()
print "The line " + line + " was split into " + len(w) + " size"
for i in range(0,len(w)-1):
if not li.startswith("#") and not li.isspace():
sheet.write(row,col,w[i])
#x = x + 1
col = col + 1
if col > 256:
print "Line of " + li
print "Row: " + row
print "Col: " + col
raise ValueError('Too many columns, can\'t insert ' + w)
row = row + 1
wbk.save('new.xls')
Reasoning is that I believe you should be changing column every time you write a value, since that appears to be causing your current error. I also believe You should be changing row after each line, not just at the end of the code which then appears superfluous
[Edit update after user feedback]

Related

Python - reading text file delimited by semicolon, ploting chart using openpyxl

I have copied the text file to excel sheet separating cells by ; delimiter.
I need to plot a chart using the same file which I achieved. Since all the values copied are type=str my chart gives me wrong points.
Please suggest to overcome this. Plot is should be made of int values
from datetime import date
from openpyxl import Workbook,load_workbook
from openpyxl.chart import (
LineChart,
Reference,
Series,
)
from openpyxl.chart.axis import DateAxis
excelfile = "C:\Users\lenovo\Desktop\how\openpychart.xlsx"
wb = Workbook()
ws = wb.active
f = open("C:\Users\lenovo\Desktop\sample.txt")
data = []
num = f.readlines()
for line in num:
line = line.split(";")
ws.append(line)
f.close()
wb.save(excelfile)
wb.close()
wb = load_workbook(excelfile, data_only=True)
ws = wb.active
c1 = LineChart()
c1.title = "Line Chart"
##c1.style = 13
c1.y_axis.title = 'Size'
c1.x_axis.title = 'Test Number'
data = Reference(ws, min_col=6, min_row=2, max_col=6, max_row=31)
series = Series(data, title='4th average')
c1.append(series)
data = Reference(ws, min_col=7, min_row=2, max_col=7, max_row=31)
series = Series(data, title='Defined Capacity')
c1.append(series)
##c1.add_data(data, titles_from_data=True)
# Style the lines
s1 = c1.series[0]
s1.marker.symbol = "triangle"
s1.marker.graphicalProperties.solidFill = "FF0000" # Marker filling
s1.marker.graphicalProperties.line.solidFill = "FF0000" # Marker outline
s1.graphicalProperties.line.noFill = True
s2 = c1.series[1]
s2.graphicalProperties.line.solidFill = "00AAAA"
s2.graphicalProperties.line.dashStyle = "sysDot"
s2.graphicalProperties.line.width = 100050 # width in EMUs
##s2 = c1.series[2]
##s2.smooth = True # Make the line smooth
ws.add_chart(c1, "A10")
##
##from copy import deepcopy
##stacked = deepcopy(c1)
##stacked.grouping = "stacked"
##stacked.title = "Stacked Line Chart"
##ws.add_chart(stacked, "A27")
##
##percent_stacked = deepcopy(c1)
##percent_stacked.grouping = "percentStacked"
##percent_stacked.title = "Percent Stacked Line Chart"
##ws.add_chart(percent_stacked, "A44")
##
### Chart with date axis
##c2 = LineChart()
##c2.title = "Date Axis"
##c2.style = 12
##c2.y_axis.title = "Size"
##c2.y_axis.crossAx = 500
##c2.x_axis = DateAxis(crossAx=100)
##c2.x_axis.number_format = 'd-mmm'
##c2.x_axis.majorTimeUnit = "days"
##c2.x_axis.title = "Date"
##
##c2.add_data(data, titles_from_data=True)
##dates = Reference(ws, min_col=1, min_row=2, max_row=7)
##c2.set_categories(dates)
##
##ws.add_chart(c2, "A61")
### setup and append the first series
##values = Reference(ws, (1, 1), (9, 1))
##series = Series(values, title="First series of values")
##chart.append(series)
##
### setup and append the second series
##values = Reference(ws, (1, 2), (9, 2))
##series = Series(values, title="Second series of values")
##chart.append(series)
##
##ws.add_chart(chart)
wb.save(excelfile)
wb.close()
I have modified below code in for loop and it worked.
f = open("C:\Users\lenovo\Desktop\sample.txt")
data = []
num = f.readlines()
for line in num:
line = line.split(";")
new_line=[]
for x in line:
if x.isdigit():
x=int(x)
new_line.append(x)
else:
new_line.append(x)
ws.append(new_line)
f.close()
wb.save(excelfile)
wb.close()
For each list,for each value check if its a digit, if yes converts to integer and store in another list.
Using x=map(int,x) didnt work since I have character values too.
I felt above is much more easy than using x=map(int,x) with try and Except
Thanks
Basha

Q: Python3 - If Statements for changing list lengths

I am attempting to analyze data sets as lists of differing lengths. I am calling lines (rows) of my data set one by one to be analyzed by my function. I want the function to still be run properly regardless of the length of the list.
My Code:
f = open('DataSet.txt')
for line in iter(f):
remove_blanks = ['']
entries = line.split()
''.join([i for i in entries if i not in remove_blanks])
trash = (entries[0], entries[1])
time = int(entries[2])
column = [int(v) for v in entries[3:]]
def myFun():
print(entries)
print_string = ''
if column[0] == 100:
if column[1] >= 250 and column[2] == 300:
if len(column) >= 9:
digit = [chr(x) for x in column[4:9]]
print_string = ('code: ' + ''.join(str(digit[l]) for l in range(5)) + ' ')
if len(column) >= 13:
optional_digit = [chr(d) for d in column[9:13]]
for m in range(0, 4):
print_string += 'Optional Field: ' + optional_digit[m] + ''
else:
print_string += 'No Optional Field '
pass
pass
print(print_string)
print('')
myFun()
f.close()
What is happening is if the length of a line of my data is not long enough (i.e. the list ends at column[6]), I get the error:
line 17, in function
print('Code: ' + digit[l])
IndexError: list index out of range
I want it to still print Code: #number #number #number #number and leave any non-existent columns as blanks when it is printed so that one line may print as Code: ABC9 and the next print as Code: AB if there are differing list lengths.
Please help! :)
Well, just make sure you're not looping over a list longer than available:
print_string = 'code: ' + ''.join(str(digit[l]) for l in range(min(5, len(digit)))) + ' '
or better:
print_string = "code {} ".format("".join(str(dig) for dig in digit[:5]))
Although I have a feeling you're over-complicating this.

web2py append dictionary to list

I create a list of dictionaries from excel. The code is listed below. What happens is the list contains the last row excel values for all the rows. I tried in python shell. It works fine. Why all the rows get updated with last row values?
d = {}
l = []
up = os.path.join(request.folder,'uploads')
workbook = xlrd.open_workbook(os.path.join(request.folder,'uploads','meas.xls'))
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
num_cells = worksheet.ncols - 1
curr_row = -1
while curr_row < num_rows:
curr_row += 1
row = worksheet.row(curr_row)
#print 'Row:', curr_row
curr_cell = -1
while curr_cell < num_cells:
curr_cell += 1
# Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank
cell_type = worksheet.cell_type(curr_row, curr_cell)
cell_value = worksheet.cell_value(curr_row, curr_cell)
#print ' ', cell_type, ':', cell_value
if curr_cell == 0:
d['loc_of_work'] = cell_value
if curr_cell == 1:
d['n'] = cell_value
if curr_cell == 2:
d['t'] = cell_value
if curr_cell == 3:
d['l'] = cell_value
if curr_cell == 4:
d['b'] = cell_value
if curr_cell == 5:
d['d'] = cell_value
print 'dict'
print d.items()
l.append(d)
print 'len of list:'
print len(l)
print 'list:'
for i,j in enumerate(l):
print i,j
The issue is you are declaring d outside the while loop, which means within the loop you are simply overwriting the same dict with new values on every iteration. Your list simply contains multiple references to the same dict object, which contains the values from the last row because those are the last values to be written to the dict (all previous values are overwritten)
moveoing:
d = {}
inside the first while loop should fix your issue

Getting a 2-D list and adding each column

I'm trying to create a function where a user inputs the values for a 3x4 2-D list, then the function prints the grid of numbers, along with an extra row at the bottom with the sum of each column. At the moment, I can't even get it to take any inputs without getting errors. Here's what I've got so far, without the addition of the columns at the end.
def testMatrixFunctions():
row0 = raw_input("Enter a 3-by-4 matrix row for row 0: ")
row0.split()
map(float, row0.split())
[float(c) for c in row0.split()]
row1 = raw_input("Enter a 3-by-4 matrix row for row 1: ")
row1.split()
map(float, row1.split())
[float(c) for c in row1.split()]
row2 = raw_input("Enter a 3-by-4 matrix row for row 2: ")
row2.split()
map(float, row2.split())
[float(c) for c in row2.split()]
print row0
print row1
print row2
You can use numpy to get the column sum
import sys
import numpy
ROWS = 3
COLS = 4
def main():
global ROWS
global COLS
matrixData = []
# Get input and process it
for i in range(0, ROWS):
thisRow = raw_input('Enter a 3-by-4 matrix row for row ' + str(i) + ' : ').split()
evalRow = []
[evalRow.append(eval(element)) for element in thisRow]
matrixData.append(evalRow)
numpyMatrix = numpy.array(matrixData)
colSums = []
[colSums.append(numpy.sum(numpyMatrix[:,index])) for index in range(0,COLS)]
# Print the matrix
for row in matrixData:
print row
print colSums
return
if __name__ == '__main__':
main()
sys.exit(0)

Python 2.7 for loop confusion

I'm trying to build a table from user input to export via Cheetah to fill a template to use as a report. I'm having trouble separating each iteration of the loop
"for j in range(1, numErrors):" and put table row tags at the beginning and end of each concatenation.
table = ""
cells = ""
row = ""
numMeas = int(raw_input("Enter total number of measurements: "))
numMeas = numMeas + 1 #number of measurements compensated for iteration behavior
for i in range(1, numMeas):
typeMeas = raw_input("Enter type of measurement "+str(i)+": ")
numErrors = int(raw_input("Enter number of error sources: "))
numErrors = numErrors + 1
for j in range(1, numErrors): #builds dataSet from number of errors
inputData = []
inputData.append(typeMeas)
description = raw_input("Enter source of uncertainty "+str(j)+": ")
inputData.append(description)
estUncert = raw_input("Enter estimated uncertainty "+str(j)+": ")
estUncert = float(estUncert)
inputData.append(str(estUncert))
for i in inputData:
cell = "<td>"+str(i)+"</td>"
cells += cell
table = "<tr>"+cells+"</tr>"+"\n"
print table
Current output:
<tr><td>mass</td><td>scale</td><td>1.0</td><td>mass</td><td>human</td><td>2.0</td> <td>temp</td><td>room</td><td>3.0</td><td>temp</td><td>therm</td><td>4.0</td></tr>
Desired output:
<tr><td>mass</td><td>scale</td><td>1.0</td></tr>
<tr><td>mass</td><td>human</td><td>2.0</td></tr>
<tr><td>temp</td><td>room</td><td>3.0</td></tr>
<tr><td>temp</td><td>therm</td><td>4.0</td></tr>
I am guessing it probably needs to look like this:
table = ""
cells = ""
row = ""
numMeas = int(raw_input("Enter total number of measurements: "))
numMeas = numMeas + 1 #number of measurements compensated for iteration behavior
for i in range(1, numMeas):
typeMeas = raw_input("Enter type of measurement "+str(i)+": ")
numErrors = int(raw_input("Enter number of error sources: "))
numErrors = numErrors + 1
inputData = []
for j in range(1, numErrors): #builds dataSet from number of errors
inputData.append(typeMeas)
description = raw_input("Enter source of uncertainty "+str(j)+": ")
inputData.append(description)
estUncert = raw_input("Enter estimated uncertainty "+str(j)+": ")
estUncert = float(estUncert)
inputData.append(str(estUncert))
cells = ''
for i in inputData:
cell = "<td>"+str(i)+"</td>"
cells += cell
table += "<tr>"+cells+"</tr>"+"\n"
print table