2017-07-06 5 views
-1

read.txtのようにカンマで数字を取得しました。 1,1,1,1 \ n個のn \ 2,2,2,2 3,3,3,3Python]最初の 'read' txtから 'write'に値を変更する方法txt

と私は、プログラムによって各値を変更したいと私は新しい 値を置き換えたいですto 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は、分割したいのPython 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

あなたの答えに感謝hava良い日の笑 –

関連する問題