2017-08-17 135 views
-1

配售變量從我的CSV片閱讀時我收到的整數誤差。它給我閱讀最後一欄的問題。我知道最後一列中的字符,但我如何將digit定義爲字符。 API函數psspy.two_winding_chg_4需要使用單引號' '輸入如在函數(所述陣列的第三元件)下面所示在單引號

excel sheet

Traceback (most recent call last): 
File "C:\Users\RoszkowskiM\Desktop\win4.py", line 133, in <module> 
psspy.two_winding_chng_4(from_,to,'%s'%digit,[_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i],[_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f, max_value, min_value,_f,_f,_f],[]) 
File ".\psspy.py", line 25578, in two_winding_chng_4 
TypeError: an integer is required 
ValueError: invalid literal for int() with base 10: 'T1' 

的代碼:

for row in data: 
    data_location, year_link, from_, to, min_value,max_value,name2,tla_2,digit = row[5:14] 
    output = 'From Bus #: {}\tTo Bus #: {}\tVMAX: {} pu\tVMIN: {} pu\t' 
    if year_link == year and data_location == location and tla_2==location: 
     from_=int(from_) 
     to=int(to) 
     min_value=float(min_value) 
     max_value=float(max_value) 
     digit=int(digit) 
     print(output.format(from_, to, max_value, min_value)) 
     _i=psspy.getdefaultint() 
     _f=psspy.getdefaultreal() 
     psspy.two_winding_chng_4(from_,to,'%s'%digit,[_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i],[_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f, max_value, min_value,_f,_f,_f],[]) 
+0

'T1'是一個字符串。你需要刪除'T'才能將其轉換爲int。在這種情況下,您可以只取該字符串的最後一個元素。 'INT(項[-1])'' – Alex

回答

0

最簡單的和可能最可用的選項是使用您自己的功能來僅過濾數字。例如:

def return_digits(string): 
    return int(''.join([x for x in string if x.isdigit()])) 
+0

致命:電路標識符是兩個以上的字符(002069) 回溯(最近最後調用): psspy.two_winding_chng_4(from_,到 「 '{}'」 的格式。 (數字),[_ I,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i],[_女,_f,_f,_f,_f,_f,_f ,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,MAX_VALUE,MIN_VALUE,_f,_f,_f],[]) 文件 「\ psspy.py」,線25588,in two_winding_chng_4 psspy.Two_Winding_Chng_4Error:two_winding_chng_4錯誤:ierr = 2 無法打開Python文件:C:\ Users \ RoszkowskiM \ Desktop \ win4.py 執行Python文件:C:\ Users \ RoszkowskiM \ Desktop \ win4 .py' – Mark

+0

對不起,在假期幾天。顯然,這隻需要應用於包含數據的列,如'N'列。這似乎來自API。你可能需要在那裏實現它。奇怪,你得到一個文件錯誤,雖然... – Troma78