How to resolve ValueError in python? - python

I am running one code in python2.7.9 so code is working fine but the same code i am running in python2.7.5 it is giving me value error.
Can you please help to to resolve this?
code:
import os
import sys
table = sys.argv[1] #CALL_KEY
column_nm = sys.argv[2] #ID
file_loc = '/medaff/eureka/RawLayer/AMPIL/'
lkp_file_path ='/medaff/eureka/AMPIL_INCREMENT/'
tgt_file_path ='/medaff/eureka/AMPIL_UNMATCHED/'
def get_col_index(file,col):
header=open(file).readline().strip()
header_list = header.split('|')
return str(header_list.index(col)+1)
src_file_nm = file_loc+table+'/'+table+'.txt'
lkp_file = lkp_file_path+table+'/'+table+'.txt'
trgt_file_nm = tgt_file_path+table+'/'+table+'_Unmatched.txt'
print(lkp_file)
rm_str = 'rm -f '+trgt_file_nm
print(rm_str)
matcher_str = 'awk -F \'|\' \'NR==FNR && NR>1{id[toupper($' + get_col_index(lkp_file,column_nm) + ')]; next} !(toupper(' + get_col_index(src_file_nm,column_nm) + ') in id)\' '+lkp_file+' '+src_file_nm+'|tail -n +2 >> '+trgt_file_nm
os.system(matcher_str)
error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in get_col_index
ValueError: 'ID' is not in list

Related

Syntax error with exec call to an object in Python 3

I really don't understand what causes the problem, could someone point it out for me please?
with shelve.open(obj_path) as obj:
for as_num in obj['as_number_list']: # ignore warning, obj['as_number_list'] is a list
temp = charge_as(obj['as_' + str(as_num)]) # temp is an object
as_test = temp # doing like this is ok
print(type(as_test))
exec("as_{}_obj = {}".format(as_num, temp)) # **error here**
And it gives syntax error like this:
<class 'instruments.AS'>
Traceback (most recent call last):
File "...", line 45, in <module>
exec("as_{}_obj = {}".format(as_num, temp))
File "<string>", line 1
as_1_obj = <instruments.AS object at 0x000002A86732E290>
^
SyntaxError: invalid syntax
I tried
exec("as_{}_obj = {}".format(as_num, temp.__dict__))
no error is shown but now as_{}_obj is of class 'dict' instead of class 'instruments.AS'
line 45:
exec("as_{}_obj = temp".format(as_num))

how to export WBlock by using pyautocad

hi all i have this problem when i try to export object through WBlock what is wrong there i'm try to do simple python work (note: i'm beginners :D) .. any help
from pyautocad import Autocad, APoint,utils
import win32com.client
AutoCAD = win32com.client.dynamic.Dispatch("AutoCAD.Application")
acad = Autocad(create_if_not_exists = False)
acad.Visible=True
doc = AutoCAD.ActiveDocument
layersList = doc.Layers
for l in layersList:
object = acad.iter_objects()
if l.name == "0":
pass
else:
for o in object:
if o.ObjectName == "AcDbText":
SelectionSet = doc.SelectionSets.Item(o.ObjectName).Name
directoryN = "C:\\Temp\\{}_{}.dwg".format(l.name,o.TextString)
doc.WBlock(directoryN,SelectionSet)
here what i Get
AcDbText
Traceback (most recent call last):
File "C:/Temp/Exporter.py", line 23, in <module>
doc.WBlock(directoryN,SelectionSet)
File "<COMObject <unknown>>", line 2, in WBlock
pywintypes.com_error: (-2147352571, 'Type mismatch.', None, 2)
thanks
i'm try to export every text as WBlock

Javascript grammar in antlr4 and python

I have used the following link for JavaScript grammar .
https://github.com/antlr/grammars-v4/tree/master/javascript/javascript/Python
i have used antlr4.8 and ntlr4-python3-runtime==4.8. when i use the following code it gives following error .
error:
PS N:\antlr4\sample\py4.8> python main.py test.js
Running
Test started for: test.js
Created parsers
Traceback (most recent call last):
File "main.py", line 25, in <module>
main(sys.argv)
File "main.py", line 20, in main
tree = parser.program()
File "N:\antlr4\sample\py4.8\JavaScriptParser.py", line 880, in program
self.enterRule(localctx, 0, self.RULE_program)
File "C:\Users\root\AppData\Local\Programs\Python\Python38\lib\site-packages\antlr4\Parser.py", line 366, in enterRule
self._ctx.start = self._input.LT(1)
.
.
.
File "N:\antlr4\sample\py4.8\JavaScriptLexer.py", line 919, in sempred
return pred(localctx, predIndex)
File "N:\antlr4\sample\py4.8\JavaScriptLexer.py", line 925, in HashBangLine_sempred
return this.IsStartOfFile()
NameError: name 'this' is not defined
code:
import sys
from antlr4 import *
import JavaScriptLexer
import JavaScriptParser
JSL = JavaScriptLexer.JavaScriptLexer
JSP = JavaScriptParser.JavaScriptParser
class WriteTreeListener(ParseTreeListener):
def visitTerminal(self, node:TerminalNode):
print ("Visit Terminal: " + str(node) + " - " + repr(node))
def main(argv):
input_stream = FileStream(argv[1])
print("Test started for: " + argv[1])
lexer = JSL(input_stream)
stream = CommonTokenStream(lexer)
parser = JSP(stream)
print("Created parsers")
tree = parser.program()
ParseTreeWalker.DEFAULT.walk(WriteTreeListener(), tree)
if __name__ == '__main__':
print("Running")
main(sys.argv)
print("Hello")
What have I done wrong ?
Replace this with self
(ref here). Example:
Change
return this.IsStartOfFile()
to
return self.IsStartOfFile().
Run the script transformGrammar.py, as indicated in documentation.

string index out of range? - python

im buliding a chat and i'm keep getting the next error:
Traceback (most recent call last):
File "C:/Users/ronen/Documents/CyberLink/tryccc.py", line 651, in <module>
main()
File "C:/Users/ronen/Documents/CyberLink/tryccc.py", line 630, in main
print_message(data_from_server, len(temp_message), username)
File "C:/Users/ronen/Documents/CyberLink/tryccc.py", line 265, in print_message
temp_l = data[0]
IndexError: string index out of range
i am trying to get the first char of the data string and convert it into int but i get this error
the problem is in the first line of the code
def print_message(d_temp, line_length, this_username):
temp_l = d_temp[0] #the problematic line
len_username = int(temp_l)
username_sender = d_temp[1:(len_username + 1)]
message_sent = d_temp[(len_username + 1): -4]
hour_time = d_temp[-4: -2]
min_time = d_temp[-2:]
printed_message = "\r" + hour_time + ":" + min_time + " " + username_sender + " : " + message_sent
print printed_message, # Prints this message on top of what perhaps this client started writing.
# if this client started typing message
complete_line_with_space(len(printed_message), line_length)
data- the data (string) from the server
line_length - the length of the temp massage
this_username - the client's username
thank you to all the helpers
empty d_temp will give this error.
This might be the reason:
>>> d_temp = ""
>>> d_temp[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
This happens because print_message is executed with an empty string as first parameter
print_message("",...)
This means the problem is somewhere else in your code.

How to fix a AttributeError in Python?

I don't success to fix this error on my code. Python wrote me this error message:
"Traceback (most recent call last):
File "C:\Users\ChloƩ CHAUMETON\Documents\BioAxial\1_R&D\4_Software\2_Python\Measurement_script\Matlab_to_Python\2019\Chloe_script_V3.py", line 114, in <module>
if 0 < len((lrir)(lrir.name_file)):
AttributeError: 'function' object has no attribute 'name_file' "
I defined the "name_file = 'BSR_Main.exe2018_09_14.csv' " up of my code but I don't understand why python don't attribute this name fto the function. Here is my code.
i_r=[]
def lrir():
lrir = liste_record[i_r]
name_file = 'BSR_Main.exe2018_09_14.csv';
for i_r in range(0,rec-1):
if 0 < len((lrir)(lrir.name_file)):
str1 = ligne.find(lrir.name_file,'/')
pr_rep = os.path.join(dir_PR,s(i_r).name_file)
log_rep = os.path.join(pr_rep,'Log')
log_file = glob.glob(os.path.join(log_rep,'*.log'))
print('log file:',log_file)

Categories

Resources