2017-07-06 73 views
-1

read.txt得到了這樣的逗號數字。 1,1,1,1 \ n 2,2,2,2 \ n 3,3,3,3Python]如何修改從第一個'讀'txt值到'寫'txt

和我想由程序改變每個值和我想更換新 值write.txt

我在很多方面做了,但我不知道如何找到逗號。

infile = open("read.txt", "r") 
outfile = open("write.txt", "w") 

total1 =0 
count1 =0 

line = infile.readline() 
while line !="": 
    value = float(line) 
    total1 = total1 + value 
    count1 = count1 + 1 
    line = infile.readline() 

outfile.write("Column,Sum,Mean,STD,Median,Mode,Min,Max\n") 


infile.close() 
outfile.close() 
+0

我真的,真的推薦看看'csv'模塊。 –

+0

是的,但在我使用該模塊之前,我想完成這種方式來理解它。 –

回答

0

首先檢查出蟒蛇CSV模塊https://docs.python.org/3.6/library/csv.html

但對於你原來的問題,如果你是在一個CSV文件中讀取,並希望得到每個「價值」要拆分,喜歡的東西

f = open("someFile.csv") 
for line in f:        # Loop iterates through file by grabbing text seperated by \n 
    listOfValuesInLine = line.split(",") #Returns a list of strings that were sepearted by "," 
    print(listOfValuesInLine)    #Prints something like ["value1","value2","value3"] etc 
+0

謝謝你的回答哈娃,美好的一天大聲笑 –