'FieldMissingError' when trying to write row - python

I keep getting a FieldMissingError on a field ('severa_id') of which I am sure that it exists. I've checked ver_33.py which shows that the Exception triggers if the field is not in self._meta.fields.
table._meta.fields shows the field being there:
print(table._meta.fields)
>>>
['proj_code', 'severa_id', 'rec_id', 'ext_key']
>>>
This is the code I'm trying:
table = dbf.Table(path_to_dbf)
table.open()
for row in dbf.Process(table):
for project in projects:
if str(row.proj_code)[0:4] == project["ProjectNumber"]:
row.write_record(severa_id=project["GUID"])
I've also tried these methods of setting the field:
row.severa_id = project["ProjectNumber"]
#row.write()
row.write_record()
Lastly, I've also tried setting each of the other fields (with a random string) which results in the same error.
EDIT: I am using the dbf module (https://pypi.python.org/pypi/dbf/0.96.005)
EDIT: The original traceback:
Traceback (most recent call last):
File "<string>", line 420, in run_nodebug
File "C:\Users\Alexander\Documents\update_projecten\update_dbf.py", line 58, in <module>
row.write()
File "C:\Users\Alexander\Envs\vfp\lib\site-packages\dbf\ver_33.py", line 2451, in __getattr__
raise FieldMissingError(name)
dbf.ver_33.FieldMissingError: 'write: no such field in table'
EDIT: The final version of the script that worked. Note not using Process and indicating in dbf.write the row and field to be written.
table = dbf.Table(path_to_dbf)
table.open()
for row in table:
for project in projects:
if str(row.proj_code)[0:4] == project["ProjectNumber"]:
dbf.write(row, severa_id=project["GUID"])

write_record no longer exists as a row method, so the error you are seeing is probably stating that write_record is not a field.
Instead, try:
dbf.write(severa_id=project['GUID'])

Related

KeyError: 'variant' while getting data from API , even tho variant data exists on API

[Here is the API snippet. I am working on]
Here is the code snippet that fetching API snippet above.
for i in parse_json["lines"]:
list = []
if (i["chaosValue"] > chaos_ex_ratio):
value = (i["variant"],i["baseType"], i["exaltedValue"], "Exalted Orb", i["levelRequired"])
else:
value = (i["variant"],i["baseType"], i["exaltedValue"], "Chaos Orb", i["levelRequired"])
list.append(value)
If I remove i["variant"] everything works perfectly but i["variant"] is throwing key error even tho "variant": "Shaper/Redeemer" exists on the JSON file. Thanks for any help.
Error code:
Traceback (most recent call last):
File "C:\Users\emosc\PycharmProjects\GithubPushs\psychescape_price_fetcher\psychescape_price_fetcher\main.py", line 265, in <module>
BaseType_Values()
File "C:\Users\emosc\PycharmProjects\GithubPushs\psychescape_price_fetcher\psychescape_price_fetcher\main.py", line 219, in BaseType_Values
value = (i["variant"],i["baseType"], i["exaltedValue"], "Exalted Orb", i["levelRequired"])
KeyError: 'variant'
You're showing us a screenshot where the first line has a variant, but your code will fail unless every line has a variant.
What is the output of this snippet?
for l in parse_json["lines"]:
if "variant" not in l:
print("Line {} does not have a 'variant'".format(l))
If it outputs any lines, there's your problem. If it doesn't, something deeper is going on. I would try to catch the KeyError exception and put a breakpoint in the handler to continue investigating.

How to delete record from dbf file using python dbf module?

I'm trying to write/delete records in a visual foxpro 6 dbf file, using python 2.7 and the dbf package:
import dbf
tbl = dbf.Table('test.dbf')
tbl.open()
rec = tbl[0]
print(rec.__class__)
rec.delete_record()
Result:
<class 'dbf.ver_2.Record'>
Traceback (most recent call last):
File "C:/Python/Projects/test/test.py", line 11, in <module>
rec.delete_record()
File "C:\Python\Projects\test\venv\lib\site-packages\dbf\ver_2.py", line 2503, in __getattr__
raise FieldMissingError(name)
dbf.ver_2.FieldMissingError: 'delete_record: no such field in table'
Here is the documentation for that package: http://pythonhosted.org/dbf/
The record object really does not have this method, but it is documented. The table is opened in read-write mode. (But it is also true that the Table() constructor should return an opened table, but it returns a closed table instead.)
What am I doing wrong?
The biggest problem is that there are no other options. The only other package I know of is "dbfpy" but that does not handle vfoxpro 6 tables, and it does not handle different character encodings.
That documentation is out of date. (My apologies.)
What you want is:
dbf.delete(rec)
tbl.pack()
Alternatively you can use dbfpy module which allows you to access records public variable deleted. Example code:
from dbfpy import dbf
...
dbfFile = dbf.Dbf("DbfFileName", readOnly=True)
for rec in dbfFile:
if rec.deleted:
#action if deleted
dbfFile.close()

cannot write cell values in Smartsheet

I've been able to read sheets, rows, columns, and cells using the Python SDK for Smartsheet, but I haven't been able to actually change/write/update a cell value. I've simplified my code quite a bit and I'm left with this:
import smartsheet
MySS = smartsheet.Smartsheet(MyApiKey)
single_row = MySS.Sheets.get_row(SHEET_ID, ROW_ID)
destination_cell = single_row.get_column(DST_COLUMN_ID)
destination_cell.value = "new value"
single_row.set_column(destination_cell.column_id, destination_cell)
MySS.Sheets.update_rows(SHEET_ID, ROW_ID)
I get the following error when I run this code:
Traceback (most recent call last):
File "C:/Users/XXXXXX/Python/Smartsheet/test.py", line 24, in <module>
MySS.Sheets.update_rows(SHEET_ID, ROW_ID)
File "C:\Users\XXXXXX\Python\virtualenv PC Smartsheet\lib\site-packages\smartsheet\sheets.py", line 961, in update_rows
for item in _op['json']:
TypeError: 'long' object is not iterable
I have tried passing the ROW_ID in the last line of code as ROW_ID and [ROW_ID] and [ROW_ID,] but get the same error nonetheless.
I'm using this as my reference: http://smartsheet-platform.github.io/api-docs/?python#update-row(s)
What am I doing wrong?
You're so close! Rather than sending the ROW_ID to the update_rows() you actually want to send the row object in a list.
So, in your case, you would just want to change your last line to be
MySS.Sheets.update_rows(SHEET_ID, [single_row])

Python - Generating RSS with PyRSS2Gen

I'm attempting to write a program that utilizes urllib2 to parse HTML, and then utilizes PyRSS2Gen to create the RSS feed, in XML.
I keep getting the error
Traceback (most recent call last):
File "pythonproject.py", line 46, in <module>
get_rss()
File "pythonproject.py", line 43, in get_rss
rss.write_xml(open("cssnews.rss.xml", "w"))
File "build/lib/PyRSS2Gen.py", line 34, in write_xml
self.publish(handler)
File "build/lib/PyRSS2Gen.py", line 380, in publish
item.publish(handler)
File "build/lib/PyRSS2Gen.py", line 427, in publish
_opt_element(handler, "title", self.title)
File "build/lib/PyRSS2Gen.py", line 58, in _opt_element
_element(handler, name, obj)
File "build/lib/PyRSS2Gen.py", line 53, in _element
obj.publish(handler)
AttributeError: 'builtin_function_or_method' object has no attribute 'publish'
upon trying to run it.
From what I could find, other users came across this issue when trying to create a new tag for the XML, but I am trying to use the default tags given with PyRSS2Gen. Inspecting the PyRSS2Gen.py file shows the write_xml() command I am using, so is the error with how I am assigning values to the rss items by popping them from a list?
def get_rss():
sys.path.append('build/lib')
from PyRSS2Gen import RSS2, RSSItem
rss = RSS2(
title = 'Python RSS Creator',
link = 'technews.acm.org',
description = 'Creates RSS out of HTML',
items = [],
)
for x in range(0, len(rssTitles)):
rss.items.append(RSSItem(
title = rssTitles.pop,
link = rssLinks.pop,
description = rssDesc.pop,
))
rss.write_xml(open("cssnews.rss.xml", "w"))
# 5 - Call function
get_rss()
I ended up just writing out to a file, like so;
news = open("news.rss.xml", "w")
news.write("<?xml version=\"1.0\" ?>")
news.write("\n")
news.write("<rss xmlns:atom=\"http://www.w3.org/2005/Atom\" version=\"2.0\">")
news.write("\n")
news.write("<channel>")
news.write("\n")
etc.
PyRSS2Gen relies on its inputs to either be strings or to have a publish method that does all the necessary conversion.
In this case, you missed to call the pop method on the rssTitles, giving you a function rather than a string. Adding () after all the pop mentions should give you a usable program.
Note that similar errors can also crop up when there's other non-sting items around (eg. byte strings); the AttributeError line gives you a hint as to the object that went into the RSS item, and the backtrace indicates where in the RSS item that is (the title, in this case).

Can't find SalesForce Object

I'm having some trouble with SalesForce, I've never used it before so I'm not entirely sure what is going wrong here. I am using the simple_salesforce python module. I have successfully pulled data from SalesForce standard objects, but this custom object is giving me trouble. My query is
result = sf.query("Select Name from Call_Records__c")
which produces this error:
Traceback (most recent call last):
File "simple.py", line 15, in <module>
result = sf.query("Select Name from Call_Records__c")
File "/usr/local/lib/python2.7/dist-packages/simple_salesforce/api.py", line 276, in query
_exception_handler(result)
File "/usr/local/lib/python2.7/dist-packages/simple_salesforce/api.py", line 634, in _exception_handler
raise exc_cls(result.url, result.status_code, name, response_content)
simple_salesforce.api.SalesforceMalformedRequest: Malformed request https://sandbox.company.com/services/data/v29.0/query/?q=Select+Name+from+Call_Records__c. Response content: [{u'errorCode': u'INVALID_TYPE', u'message': u"\nSelect Name from Call_Records__c\n ^\nERROR at Row:1:Column:18\nsObject type 'Call_Records__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name.
Please reference your WSDL or the describe call for the appropriate names."}]
I've tried it with and without the __c for both the table name and the field name, still can't figure this out. Anything blatantly wrong?
Make sure your result is Call_Records__c/CallRecords__c
Call_Records__c result = sf.query("Select Name from Call_Records__c")
Or
CallRecords__c result = sf.query("Select Name from CallRecords__c")
Try using -
result = sf.query("Select Name from CallRecords__c")

Categories

Resources