Python string wrong characters - python

let me start by saying that I attempted to create a minimal example but was unable to replicate the results. I try to not make it a habbit to dump all my code on here but I am not sure of another way to illustrate the issue. I am writing a class to output data from my application to a specific format and I am getting the wrong results in the code.
The format I am attempting to output should look like this:
Nodestyle "nodeType" "name"
"attribute-type attribute-name" [value]
If the attribute is linked to another node
"refrence attribute-type attribute-name" ["nodename:output-attribute-name"]
Example:
Bxdf "PxrDisney" "7fcfd465-87c3-4092-98b9-b67d1655bc32"
"color baseColor" [0.20.50.8]
"color emitColor" [0.00.00.0]
"float subsurface" [0]
"reference color subsurfaceColor" ["a3bb37f2-dbb8-407b8a1ac93822ebef7c:resultRGB"]
"float metallic" [0]
"float specular" [.5]
"float specularTint" [0]
"float roughness" [.25]
"float anisotropic" [0]
"float sheen" [0]
"float sheenTint" [.5]
"float clearcoat" [0]
"float clearcoatGloss" [1]
"float presence" [1]
"int inputAOV" [0]
Pattern "PxrHSL" "a3bb37f2-dbb8-407b-8a1a-c93822ebef7c"
"color inputRGB" [0.00.00.0]
"float hue" [0.0]
"float saturation" [1.0]
"float luminance" [1.0]
The results I am getting look like this:
('Bxdf "PxrDisney" "7fcfd465-87c3-4092-98b9-b67d1655bc32" \n "color baseColor" [0.20.50.8] \n "color emitColor" [0.00.00.0] \n "float subsurface" [0] \n "reference color subsurfaceColor"', '["a3bb37f2-dbb8-407b-8a1a-c93822ebef7c:resultRGB"] \n ')"float metallic" [0]
"float specular" [.5]
"float specularTint" [0]
"float roughness" [.25]
"float anisotropic" [0]
"float sheen" [0]
"float sheenTint" [.5]
"float clearcoat" [0]
"float clearcoatGloss" [1]
"float presence" [1]
"int inputAOV" [0]
Pattern "PxrHSL" "a3bb37f2-dbb8-407b-8a1a-c93822ebef7c"
"color inputRGB" [0.00.00.0]
"float hue" [0.0]
"float saturation" [1.0]
"float luminance" [1.0]
It looks like it if outputting special characters and extra items in the string up to where there is a reference to another node. if I link to another attribute lets say basecolor it will only give the wrong results up to basecolor. If there is no connections at all there is no issue. Why would a string give this behaviour? Here is the class in question. The class take an array of node objects I step through.
#This will handel the export of the RIB contaning all the shader calls
class exporter ():
nodes = None
dicNodes = {}
outStream = []
#Method Two: Use shader as a starting point
#Find the shader
#go through each attribute on the shader
#if connected check attrs on connected node
def __init__(self, incomenodes):
self.nodes = incomenodes
dicNodes = {}
outStream = []
#create a dic using nids as keys
for node in self.nodes:
self.dicNodes[str(node.nid)] = node
#Run Export Command for each shader
x = 0
for node in self.nodes:
print str(node.nodeType.nType)
if str(node.nodeType.nType) == 'bxdf':
self.streamMe(node)
target = open('/home/jspada20/outStream.RIB', 'w')
print 'JAMES! There are ', len(self.outStream), 'items in the out stream'
for line in self.outStream:
target.write(line)
print line
target.close()
def streamMe(self, node):
#Layout for the header of a node call is....
#Pattern "PxrThinFilm" "PxrThinFilm1"
#Bxdf "PxrDisney" "PxrDisney1"
#{Type} "{Identifyer}" "{Name/UID}"
moreNodes = []
#need to fix lower case letters comming in from XML source
nodefunction = None
if str(node.nodeType.nType[0]) == 'p':
nodefunction = 'Pattern'
elif str(node.nodeType.nType[0]) == 'b':
nodefunction = "Bxdf"
else:
nodefunction = str(node.nodeType.nType)
self.outStream.append(nodefunction + ' "' + str(node.nodeType.nName) + '" ' + '"' + str(node.nid) + '" \n ')
for attr in node.inputs:
#Check to see if it is connected
#if not connected:
if attr.currentConnectedNode is None:
#Check to see what type the value is. This will govern the export scheme
if str(attr.argType) == "int":
#print '"'+ str(attr.argType), attr.name + '" [' + str(attr.value) + ']'
self.outStream[len(self.outStream) - 1] = self.outStream[len(self.outStream) - 1] + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value) + '] \n '
elif str(attr.argType) == "float":
if str(attr.value[len(str(attr.value))-1]) == '.':
outVal = str(attr.value) + '0'
else:
outVal = str(attr.value)
#print '"'+ str(attr.argType), attr.name + '" [' + outVal + ']'
self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value) + '] \n '
elif str(attr.argType) == "color":
#print '"'+ str(attr.argType), attr.name + '" [' + str(attr.value.r), str(attr.value.g), str(attr.value.b) + ']'
self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value.r) + str(attr.value.g) + str(attr.value.b) + '] \n '
elif str(attr.argType) == "string":
#print '"'+ str(attr.argType), attr.name + '" ["' + attr.value + '"]'
self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value) + '] \n '
else:
self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"reference ' + str(attr.argType) + " " + attr.name + '"', '["' + str(attr.currentConnectedNode.nid) + ':' + attr.currentConnectedOutput.name + '"] \n '
moreNodes.append(str(attr.currentConnectedNode.nid))
for ids in moreNodes:
self.streamMe(self.dicNodes[ids])

When I was converting this code from print statements to storing the information in a list, I missed a comma. Tuples can be declared like this:
myTuple = "1", "2", "3"
By having a comma in the statement I was converting the string.
Changing the comma to a + in this statement resolved the issue. Coming from c++ I was not used to mutable data types!
self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"reference ' + str(attr.argType) + " " + attr.name + '"' '["' + str(attr.currentConnectedNode.nid) + ':' + attr.currentConnectedOutput.name + '"] \n '

Related

Why is the condition performed when it shouldn't be?

I have a sequence of characters that I want to insert into a 2D table. But, despite the if condition : when I reach a character '*', it goes into the if condition, whereas it should go directly into the else.
if str(tab_2D[q + 5][1]) != '*':
print("IN")
if table_transition[int(etat1) + 1][ord(symbole) - ord('a') + 2] == " ":
table_transition[int(etat1) + 1][ord(symbole) - ord('a') + 2] = etat2
else:
table_transition[int(etat1) + 1][ord(symbole) - ord('a') + 2] += ',' + etat2
else: # si le symbole EST epsilon
if table_transition[int(etat1) + 1][int(symbol) + 2] == " ":
table_transition[int(etat1) + 1][int(symbol) + 2] = etat2
else:
table_transition[int(etat1) + 1][int(symbol) + 2] += ',' + etat2
q += 1
Here's the proof that ' * ' does go into the if condition because "IN" is displayed once when it's really a ' * ' character that is being compared. It should not be displayed.
Here's proof:
Do you know why?
I solved the problem, I had forgotten to add q += 1 before the main else

Is there a way to solve this TypeError [duplicate]

This question already has answers here:
Why do I get an IndexError (or TypeError, or just wrong results) from "ar[i]" inside "for i in ar"?
(4 answers)
Closed 6 months ago.
I am having trouble figuring out what is wrong with my code. I need help.
next(fhr) # skip header row
customer_list = [] # initialize empty customer list
for line in fhr:
ls = line.split(',')
customer_list.append([ls[0], ls[1], ls[2], ls[3], ls[4], ls[5], ls[6], int(ls[7]), ls[8], ls[9], ls[10].strip('\n')])
from operator import itemgetter
customer_list.sort(key=itemgetter(7), reverse=True)
print(customer_list)
writepath = './loan-data-output-v1.csv'
fwh = open(writepath, 'w', encoding='utf-8')
fwh.write('Name' +','+ 'State' +','+'Age' +','+'Annual Income'+','+ 'Loan Type' +','+' Loan Amount' +','+ 'Length of Loan in Years' +','+ 'Days Delinquent' +','+ 'Interest Rate' +','+ 'Number of Loans Prior' +','+'Years as Customer' + '\n')
for i in customer_list:
if customer_list[i][7] >= 90:
fwh.write(customer_list[i][0] + ',' + customer_list[i][1] + ',' + customer_list[i][2] + ',' + customer_list[i][3] + ',' + customer_list[i][4] + ',' + customer_list[i][5] + ',' + customer_list[i][6] + ',' + customer_list[i][7] + ',' + customer_list[i][8] + ',' + customer_list[i][9] + ','+ customer_list[i][10] + '\n')
fhr.close()
fwh.close()
I am getting this error for the last for loop and I'm not sure what to do about it. Can someone help.
TypeError: list indices must be integers or slices, not list
You are using lists of lists so when you use for i in list_of_list, i itself becomes a list..
for i in customer_list:
if i[7] >= '90':
fwh.write(i[0] + ',' + i[1] + ',' + i[2] + ',' + i[3] + ',' + i[4] + ',' + i[5] + ',' + i[6] + ',' + str(i[7]) + ',' + i[8] + ',' + i[9] + ','+ i[10] + '\n')
fhr.close()
fwh.close()
Alternatively you can use,
for i in range(0,len(customer_list)):
if customer_list[i][7] >= '90':
fwh.write(customer_list[i][0] + ',' + customer_list[i][1] + ',' + customer_list[i][2] + ',' + customer_list[i][3] + ',' + customer_list[i][4] + ',' + customer_list[i][5] + ',' + customer_list[i][6] + ',' + str(customer_list[i][7]) + ',' + customer_list[i][8] + ',' + customer_list[i][9] + ','+ customer_list[i][10] + '\n')
fhr.close()
fwh.close()
EDIT: Second method assumes that your length of customer_list is constant or in other words you are not adding anything to customer_list during the loop. Thanks to DanielRoseman for pointing out potential bug in second code..
EDIT 2:
Thanks to quamrana for suggesting this way,
for i in customer_list:
if i[7] >= '90':
i[7] = str(i[7])
fwh.write(','.join(i[0:11]) + '\n')
fhr.close()
fwh.close()

New line being printed as "\n" instead of actual new line

I'm trying to print a simple game board.
places = [" "] * 9
board = (places[8] + "|" + places[7] + "|" + places[6] + "\n",
places[5] + "|" + places[4] + "|" + places[3] + "\n",
places[2] + "|" + places[1] + "|" + places[0] + "\n")
print(board)
But instead of a board I get this:
(' | | \n', ' | | \n', ' | | \n')
I want to print it without the parentheses and with new lines instead of \n. How can I do this?
Don't print whole tuple, but it's elements, e.g.:
for i in board:
print(i)
You just have to use this line to print:
print(*board,sep="")
Explanation: the operator * is the unpacking operator: the tuple board is then unpacked, and it is like you've done:
print( places[8] + "|" + places[7] + "|" + places[6] + "\n",
places[5] + "|" + places[4] + "|" + places[3] + "\n",
places[2] + "|" + places[1] + "|" + places[0] + "\n",
sep="")
Because you've put commas on the end of each line in the brackets, you've made a tuple, which is an iterable, like a list. Just replace the commas with a + at the start of the next line
board = ('line 1' + '\n'
+ 'line 2' + '\n'
+ 'line 3' + '\n')
This creates a string instead of a tuple.
places = [" "] * 9
print(places[8] + "|" + places[7] + "|" + places[6] + "\n" + places[5] + "|" +
places[4] + "|" + places[3] + "\n" + places[2] + "|" + places[1] + "|" + places[0] +
"\n")
This is what you're looking for.
The problem was that "\n" only works in print statements.

Python - key error when using "if in dict"

I am receiving the following error when running a script to parse contents of an XML file.
if iteration.findtext("Iteration_query-def") in ecdict:
KeyError: 'XLOC_000434'
I was under the impression that using "if in dict" would mean that if the key is not found in the dictionary, the script will continue past the if statement and proceed with the rest of the code. Below is the problematic section of the code I am using. I realise this is quite a basic question, but I am unsure what else I can say, and I don't understand why I am receiving this error.
import xml.etree.ElementTree as ET
tree = ET.parse('507.FINAL_14.2.14_2_nr.out_fmt5.out')
blast_iteration = tree.find("BlastOutput_iterations")
for iteration in blast_iteration.findall("Iteration"):
query = iteration.findtext("Iteration_query-def").strip().strip("\n")
if query in score:
continue
if iteration.findtext("Iteration_message") == "No hits found":
if iteration.findtext("Iteration_query-def") in tair:
tairid = tair[iteration.findtext("Iteration_query-def")][0]
tairdes = tair[iteration.findtext("Iteration_query-def")][1]
else:
tairid = "-"
tairdes = "-"
goterms = ""
ecterms = ""
if iteration.findtext("Iteration_query-def") in godict:
for x in godict[iteration.findtext("Iteration_query-def")][:-1]:
goterms = goterms + x + ";"
goterms = goterms + godict[iteration.findtext("Iteration_query-def")][-1]
else:
goterms = "-"
if iteration.findtext("Iteration_query-def") in ecdict:
for x in ecdict[iteration.findtext("Iteration_query-def")][:-1]:
ecterms = ecterms + x + ";"
ecterms = ecterms + ecdict[iteration.findtext("Iteration_query-def")][-1]
else:
ecterms = "-"
if iteration.findtext("Iteration_query-def") in godescr:
desc = godescr[iteration.findtext("Iteration_query-def")]
else:
desc = "-"
n += 1
p = "PvOAK_up"+str(n) + "\t" + tranlen[iteration.findtext("Iteration_query-def")] + "\t" + orflen[iteration.findtext("Iteration_query-def")] + "\t" + "-" + "\t" + "-" + "\t" + tairid + "\t" + tairdes + "\t" + goterms + "\t" + ecterms + "\t" + desc + "\t" + str(flower[query][2]) + "\t" + str('{0:.2e}'.format(float(flower[query][1]))) + "\t" + str('{0:.2f}'.format(float(flower[query][0]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][2]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][1]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][0])))
print p
Hope you can help,
Thanks.
edit: I should say that godict and ecdict were previously created as follows - I can submit the entire code if needs be:
godict = {}
ecdict = {}
godescr = {}
f = open("507.FINAL_14.2.14_2_nr.out_fmt5.out.annot")
for line in f:
line = line.split("\t")
if len(line) > 2:
godescr[line[0]] = line[2]
line[1] = line[1].strip("\n")
if line[1].startswith("EC"):
if line[0] in ecdict:
a = ecdict[line[0]]
a.append(line[1])
ecdict[line[0]] = a
else:
ecdict[line[0]] = [line[1]]
else:
if line[0] in godict:
a = godict[line[0]]
a.append(line[1])
godict[line[0]] = a
else:
godict[line[0]] = [line[1]]
Traceback:
Traceback (most recent call last):
File "2d.test.py", line 170, in <module>
p = "PvOAK_up"+str(n) + "\t" + tranlen[iteration.findtext("Iteration_query-def")] + "\t" + orflen[iteration.findtext("Iteration_query-def")] + "\t" + "-" + "\t" + "-" + "\t" + tairid + "\t" + tairdes + "\t" + goterms + "\t" + ecterms + "\t" + desc + "\t" + str(flower[query][2]) + "\t" + str('{0:.2e}'.format(float(flower[query][1]))) + "\t" + str('{0:.2f}'.format(float(flower[query][0]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][2]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][1]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][0])))
KeyError: 'XLOC_000434'

Python: Expanding Complicated Tree DataStructure

I am exploring a data structure which get expands to sub-elements and resolves to a final element. But I only want to store top two levels.
Example: Lets say I start with New York which breaks into Bronx, Kings, New York, Queens, and Richmond as counties but then finally somehow they resolve to USA.
I am not sure if this is a good example but just to make it clear here is more clear explanation of the problem.
A (expands to) B,C,D -> B (expands to) K,L,M -> K resolves to Z
I initially wrote it in series of for loops and then use the recursion but in recursion I am loosing some of the elements that get expand and due to that I don't drill down each of the expanded element. I have put the both recursive version and non-recursive. I am looking for some advise on building this data structure, and what is the best way to do.
I call a data base query for every element in the expanded version which returns a list of items. Go until it resolves to single element. With out recursion I don't loose drilling all the way till the final element that others resolve to. But with recursion its not the same. I am also new to python so hopefully this is not a bad question to ask in a site like this.
returnCategoryQuery is a method that returns list of items by calling the database query.
With out recursion
#Dictionary to save initial category with the rest of cl_to
baseCategoryTree = {};
#categoryResults = [];
# query get all the categories a category is linked to
categoryQuery = "select cl_to from categorylinks cl left join page p on cl.cl_from = p.page_id where p.page_namespace=14 and p.page_title ='";
cursor = db.cursor(cursors.SSDictCursor);
for key, value in idTitleDictionary.iteritems():
for startCategory in value[0]:
#print startCategory + "End of Query";
categoryResults = [];
try:
categoryRow = "";
baseCategoryTree[startCategory] = [];
print categoryQuery + startCategory + "'";
cursor.execute(categoryQuery + startCategory + "'");
done = False;
while not done:
categoryRow = cursor.fetchone();
if not categoryRow:
done = True;
continue;
categoryResults.append(categoryRow['cl_to']);
for subCategoryResult in categoryResults:
print startCategory.encode('ascii') + " - " + subCategoryResult;
for item in returnCategoryQuery(categoryQuery + subCategoryResult + "'"):
print startCategory.encode('ascii') + " - " + subCategoryResult + " - " + item;
for subItem in returnCategoryQuery(categoryQuery + item + "'"):
print startCategory.encode('ascii') + " - " + subCategoryResult + " - " + item + " - " + subItem;
for subOfSubItem in returnCategoryQuery(categoryQuery + subItem + "'"):
print startCategory.encode('ascii') + " - " + subCategoryResult + " - " + item + " - " + subItem + " - " + subOfSubItem;
for sub_1_subOfSubItem in returnCategoryQuery(categoryQuery + subOfSubItem + "'"):
print startCategory.encode('ascii') + " - " + subCategoryResult + " - " + item + " - " + subItem + " - " + subOfSubItem + " - " + sub_1_subOfSubItem;
for sub_2_subOfSubItem in returnCategoryQuery(categoryQuery + sub_1_subOfSubItem + "'"):
print startCategory.encode('ascii') + " - " + subCategoryResult + " - " + item + " - " + subItem + " - " + subOfSubItem + " - " + sub_1_subOfSubItem + " - " + sub_2_subOfSubItem;
except Exception, e:
traceback.print_exc();
With Recursion
def crawlSubCategory(subCategoryList):
level = 1;
expandedList = [];
for eachCategory in subCategoryList:
level = level + 1
print "Level " + str(level) + " " + eachCategory;
#crawlSubCategory(returnCategoryQuery(categoryQuery + eachCategory + "'"));
for subOfEachCategory in returnCategoryQuery(categoryQuery + eachCategory + "'"):
level = level + 1
print "Level " + str(level) + " " + subOfEachCategory;
expandedList.append(crawlSubCategory(returnCategoryQuery(categoryQuery + subOfEachCategory + "'")));
return expandedList;
#Dictionary to save initial category with the rest of cl_to
baseCategoryTree = {};
#categoryResults = [];
# query get all the categories a category is linked to
categoryQuery = "select cl_to from categorylinks cl left join page p on cl.cl_from = p.page_id where p.page_namespace=14 and p.page_title ='";
cursor = db.cursor(cursors.SSDictCursor);
for key, value in idTitleDictionary.iteritems():
for startCategory in value[0]:
#print startCategory + "End of Query";
categoryResults = [];
try:
categoryRow = "";
baseCategoryTree[startCategory] = [];
print categoryQuery + startCategory + "'";
cursor.execute(categoryQuery + startCategory + "'");
done = False;
while not done:
categoryRow = cursor.fetchone();
if not categoryRow:
done = True;
continue;
categoryResults.append(categoryRow['cl_to']);
#crawlSubCategory(categoryResults);
except Exception, e:
traceback.print_exc();
#baseCategoryTree[startCategory].append(categoryResults);
baseCategoryTree[startCategory].append(crawlSubCategory(categoryResults));
Are you trying to lookup "Queens" and learn that it is in the USA? Have you tried encoding your tree in XML, and using lxml.etree to find an element and then use getpath to return the path in XPath format?
This would meaning adding a fourth top level to your tree, namely World, and then you would search for Queens and learn that the path to Queens is World/USA/NewYork/Queens. The answer to your question would always be the second item in the XPath.
Of course you could always just build a tree from the XML and use a tree search algorithm.

Categories

Resources