2015-01-23 4 views
0

그래서, 내가 파일 lekovi.txt에있는 텍스트를 변경하려면,하지만 변경하려는 것은 특정, 모든 행은 다음과 같이 진행됩니다파일에서 매개 변수를 하나만 변경하는 방법은 무엇입니까?

name|2ndname|price|stock|serial|False/True|number(starting from 1 and appending by 1 

내가하는 숫자 I에 따라 선택할 때마다 변경하고 싶다면 거짓에서 참으로 변경하고 싶습니다. 어떻게해야합니까?

print("\n-- Brisanje lekova -- \n") 
     print("+=============================================================================+") 
     print("| Br. | Naziv  | Gen. Naziv | Serijski br. | Kolicina|  Cena  |") 
     print("+=============================================================================+\n") 
     n = 1 
     lista_lekova = open("lekovi.txt",'r').readlines() 
     redovi = [] 
     lek={} 
     for i in lista_lekova: 
      deo = i.strip().split("|") 
      redovi.append(deo) 
      print("| ", n , " | {:13} | {:12} | {:12} | {:7} | {:6}.00 din | ".format(deo[0],deo[1],deo[2],deo[3],deo[4]))  
      n = n + 1 

      lek[0] = deo[0] 
      lek[1]= deo[1] 
      lek[2]= deo[2] 
      lek[3]= deo[3] 
      lek[4]= deo[4] 
      lek[5]= deo[5] 
      lek[6]= deo[6] 
     print("+=============================================================================+") 
     izbrisanilek = input("Lek pod kojim brojem zelite da izmenite: ") 
     izbrisaniLek = int(izbrisanilek) 
     izbrisaniLek = izbrisaniLek - 1 
     for lek in lista_lekova: 
      print (lek.deo[6]) 
      k = "23" 
      if k == izbrisaniLek: 
       ceoLek = deo[0] + "|" + deo[1] + "|" + deo[2] + "|" + deo[3] + "|" + deo[4] + "|" + "True" + "|" + deo[6] + "\n" 
       lekovistr = open("lekovi.txt" , "w") 
       lekovistr.write(ceoLek) 
       lekovistr.close() 
+0

그래서 파일 lekovi.txt에는 한 줄만 표시됩니까? – Marcin

+0

아니, 여러 줄을 가지고 있는데, 그 이유는 숫자를 넣어서 선택할 수 있기 때문입니다. 그러나 어떻게 알지는 못합니다 ... –

+0

그래서 실제로 문제는 무엇입니까? 어떤 오류? 예상되는 결과는 무엇입니까? – Marcin

답변

1

코드가 수정되었습니다.

# read a file 
with open("test.txt",'r') as f: 
    lista_lekova = open("test.txt",'r').readlines() 

# get a line number to change  
izbrisanilek = input("Lek pod kojim brojem zelite da izmenite: ") 
izbrisaniLek = int(izbrisanilek) 
izbrisaniLek = izbrisaniLek - 1 


# here will be stored output lines 
out_lines = [] 

#iterate for each line and change izbrisaniLek line 
for k,lek in enumerate(lista_lekova): 

    # the spliting can also be avoided, just to change False to True. 
    # But you seem to do some reformatting of other values. 
    deo = list(map(lambda v: v.strip(), lek.split('|'))) 

    # change False to True for the selected line. 
    # it always makes it true. Not sure if you want to toggle the values, or just always have True there. 
    if k == izbrisaniLek: 
     deo[5] = 'True' 

    ceoLek = "|".join(deo) 
    #print(ceoLek) 
    out_lines.append(ceoLek) 

# save output files to a file 
with open("lekovi_out.txt" , "w") as f: 
    f.writelines(out_lines) 

출력은 다음과 같습니다 :

name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 
name|2ndname|price|stock|serial|True|number(starting from 1 and appending by 1 
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 

는 희망이 도움이됩니다 다음과 같은 형식의 입력 파일에 대해 다음과 같이

name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1 

True로 거짓은 3 행에서 변경 될 수 있습니다 . 당신이하고 싶은 것과 왜 당신이 원하는지 아직 명확하지 않습니다. 단일 라인을 변경하기 위해 각 라인을 반복 할 필요는 없지만 라인을 수정/재구성하고있는 중이므로 반복 만 포함했습니다.

+0

그게 고맙습니다. ***** –

관련 문제