返回列表 發帖

407 字串與檔案處理 (字元刪除及另存新檔)

1. 題目說明:
請依下列題意進行作答,使輸出值符合題意要求。

2. 設計說明:
請撰寫一程式,讀取read.txt檔案內容,將檔案中的「*」符號全部刪除,並寫入至write.txt檔案。

提示:若使用 Java 語言答題,請以「JP」開頭命名包含 main 靜態方法的 class,評測系統才能正確評分。

3. 輸入輸出:
輸入說明
讀取read.txt檔案內容

輸出說明
寫入至write.txt檔案

範例輸入



範例輸出



附件:read.txt

本帖隱藏的內容需要回復才可以瀏覽


Python
  1. with open(r'read.txt', 'r') as infile, open(r'write.txt', 'w') as outfile:
  2.     data = infile.read()
  3.     data = data.replace("*", "")
  4.     outfile.write(data)
複製代碼

  1. with open(r'read (1).txt', 'r') as r:
  2.     s=r.readline()
  3.     out=""
  4.     for i in range(len(s)):
  5.         if s[i]!="*":
  6.             out=out+s[i]
  7. with open(r'write.txt','w') as w:
  8.     w.write(out)
複製代碼

TOP

  1. out=""
  2. with open ("read.txt",'r')as r:
  3.     s=r.readline()
  4.     for i in range(len(s)):
  5.         if s[i]!='*':
  6.             out+=s[i]
  7. with open ("write.txt",'w')as w:
  8.     w.write(out)
複製代碼

TOP

  1. out=""

  2. with open("read.text",'r')as r:
  3.     s=r.readline()
  4.     for i in range(len(s)):
  5.         if s[i]!='*':
  6.             out+=s[i]
  7.             
  8. with open("write.txt",'w')as w:
  9.     w.write(out)
複製代碼

TOP

  1. with open(r'read.txt','r') as r:
  2.     s=r.readline()
  3.     out=""
  4.     for i in range(len(s)):
  5.         if(s[i]!='*'):
  6.               out+=s[i]
  7. with open(r'write.txt','w') as w:
  8.     w.write(out)
複製代碼

TOP

本帖最後由 沈敬翔 於 2024-2-1 14:06 編輯
  1. ans=""
  2. with open('read.txt','r')as infile:
  3.     data=infile.readline()
  4.     for i in range(len(data)):
  5.         if data[i]!='*':
  6.            ans+=data[i]
  7. with open('write.txt','w')as outfile:
  8.     outfile.write(ans)     
複製代碼
回復 1# 鄭繼威

TOP

out=""
with open('read.txt','r') as r:
    s=r.readline()
    for i in range(len(s)):
        if s[i]!='*':
            out+=s[i]

with open('write.txt','w') as w:
    w.write(out)

TOP

返回列表