題目
讀取資料集dataset10.csv,控制臺列印輸出每行的第四個字串,并修改檔案,在檔案里每一行末尾加一個0-100的亂數,用逗號與前面的內容分開
代碼
import random
with open('此處寫入自己的檔案位置', 'r') as f:
text = []
for i in f:
text.append(i.strip('\n').split(','))
#把csv檔案讀取出來,放在串列中
with open('dataset10.csv', 'a') as f:
f.write(','.join(text[0]))
f.write('\n')
for i in text[1:]:
print(i[3])
i.append(str(random.randint(0, 100)))
f.write(','.join(i))
f.write('\n')
#修改串列中的元素,然后寫入新的CSV檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/225359.html
標籤:python
上一篇:Python入門基礎語法知識5
