2017-04-25 97 views
0

考慮到我的文件的內容:格式化來自字符串列表的字符串時出現Python錯誤?

"ID","Name","Type 1","Type 2","Generation","Legendary" 
1,"Bulbasaur","Grass","Poison",1,"FALSE" 
6,"Charizard","Fire","Flying",1,"FALSE" 
4,"Charmander","Fire","",1,"FALSE" 
169,"Crobat","Poison","Flying",2,"FALSE" 
146,"Moltres","Fire","Flying",1,"TRUE" 
643,"Reshiram","Dragon","Fire",5,"TRUE" 
641,"Tornadus, (Incarnate Form)","Flying","",5,"TRUE" 

我使用readlines方法()創建每個字符串作爲自己的行列表。

然後我試圖把這些字符串和格式化他們的格式如下:

'Bulbasaur': (1, 'Grass', 'Poison', 1, False) 

我需要知道確切的報價是正確的,全部小寫,並轉換爲大寫是正確的。我也必須確保類型被製作成他們需要的東西。

當我去重複或格式化字符串(即帶和分),我收到了一些錯誤:

TypeError: 'int' object is not iterable 
AttributeError: 'int' object has no attribute 'split' 

我,如何這需要工作認真困惑。我的整體功能運行,但沒有返回正確的結果。例如:它會返回字典中的charmander信息而不是bulbasaur的信息。

  • 我需要採取從readlines方法(結果),並獲得每一行作爲字符串
  • 我需要格式化該字符串到上文
  • 然後提供的格式一次我有格式,我需要把它變成一本字典。

這裏是我的功能,這實在是所有的地方:

def read_info_file(filename): #accept file 
    file= open(filename) 
    lines=file.readlines()[1:] #skip first header line 
    d={} 
    for line in lines: 
     split_line=line.split(',') #get individual strings 
     legendary=True 
     if 'F' == split_line[-1].strip('"')[0]: #check last position if t or f to format legendary correctly 
     legendary=False 

     if len(split_line) > 6: 
      (k,v)=(split_line[1]+split_line[2].strip('"'), #puts right order and removes excess quotations 
(int(split_line[0]),split_line[3].strip('"'),split_line[4].strip('"'), 
     int(split_line[5]),legendary)) 

     else: 
      (k,v)=(split_line[1].strip('"'), 
(int(split_line[0]),split_line[2].strip('"'),split_line[3].strip('"'), 
      int(split_line[4]),legendary)) 

    d.update([(k,v)]) 
    file.close() 
    return d 

回答

1

使用內置csv模塊簡化事情:

import csv 
from pprint import pprint 

def read_info_file(filename): 
    with open(filename,'r',newline='') as f: 
     r = csv.reader(f) 
     next(r) # skip header 
     d = {} 
     for id,name,type1,type2,generation,legendary in r: 
      d[name] = int(id),type1,type2,int(generation),legendary=='TRUE' 
    return d 

pprint(read_info_file('input.txt')) 

輸出

{'Bulbasaur': (1, 'Grass', 'Poison', 1, False), 
'Charizard': (6, 'Fire', 'Flying', 1, False), 
'Charmander': (4, 'Fire', '', 1, False), 
'Crobat': (169, 'Poison', 'Flying', 2, False), 
'Moltres': (146, 'Fire', 'Flying', 1, True), 
'Reshiram': (643, 'Dragon', 'Fire', 5, True), 
'Tornadus, (Incarnate Form)': (641, 'Flying', '', 5, True)} 
+0

我希望我能做到這一點!不幸的是我不允許使用任何導入的模塊。 –

+0

@ ocean.1234你應該寫一個解析器嗎?因爲最後一個數據項在引號中嵌入了逗號,所以一個簡單的'line.split(',')'不起作用。 –

+0

我不確定解析器是什麼? –