2

後で呼び出すことができるように、コードの出力をファイルに書き込む必要があります。元の test1 ファイルではなく、出力を呼び出す必要があります。出力を作成するコードは以下にあり、正常に動作します。後で呼び出すことができるファイルにそれを取得できません。

 import csv 

 file1  = open('C:/Users/Gallatin/Documents/adamenergy.csv',"r") #Open CSV File in Read Mode 
 reader = csv.reader(file1)      #Create reader object which iterates over lines 

 class Object:                   #Object to store unique data 
  def __init__(self, name, produce, amount): 
    self.name = name 
    self.produce = produce 
    self.amount = amount 

 rownum = 0 #Row Number currently iterating over 
 list = []  #List to store objects 

 def checkList(name, produce, amount): 


    for object in list:  #Iterate through list         
    if object.name == name and object.produce == produce:  #Check if name and produce combination exists 
        object.amount += int(amount) #If it does add to amount variable and break out 
        return 

newObject = Object(name, produce, int(amount)) #Create a new object with new name, produce, and amount 
list.append(newObject)  #Add to list and break out 


  for row in reader:  #Iterate through all the rows 
   if rownum == 0:  #Store header row seperately to not get confused 
    header = row 
  else: 
    name = row[0]  #Store name 
    produce = row[1]  #Store produce 
    amount = row[2]  #Store amount 

    if len(list) == 0:  #Default case if list = 0 
        newObject = Object(name, produce, int(amount)) 
        list.append(newObject) 
    else:  #If not... 
        checkList(name, produce, amount) 


rownum += 1 

 for each in list:
   print each.name,each.produce,each.amount

印刷すると、必要な出力が正しく生成されますが、この出力をファイルに書き込む必要があるため、後でndiffを使用して呼び出して別のcsvファイルと比較できます。上記の同様のコードを実行します

4

4 に答える 4

4

出力をファイルにリダイレクトするだけです。例えば、

C:> python myfile.py> output.txt

于 2012-06-20T02:12:39.717 に答える
4

いくつかの方法があります。

  1. プログラムを別の方法で実行することもできます。実行する代わりに:

    ./generate
    

    走る

    ./generate > output_file.csv
    

    これはシェルリダイレクトを使用して、標準出力を指定したファイルに保存します。これは非常に柔軟で、将来のスクリプトに簡単に組み込むことができます。標準入力または名前付きファイルを介して入力を受け入れる場合は特に素晴らしいです。これにより、ツールが非常に柔軟になります。

  2. プログラムを変更して、特定のファイルに書き込むことができます。次のようなファイルを開きます。

    output = open("output.csv", "w")
    

    次に、次の行に沿って文字列を使用して出力を書き込みます

    output.write(each.name + "," + str(each.produce)  + "," + str(each.amount))
    
  3. 同じcsvモジュールを使用してファイルを書き込むこともできます。,これは、文字を含む入力の複雑なケースやその他の困難なケースを自動的に処理するため、長期的な使用にはより良いアプローチかもしれません.

于 2012-06-20T02:18:48.923 に答える
2

ファイルの先頭で、出力ファイルを開きます。

outf = open("my_output_file.csv", "wb")

じゃあ後で:

print >>outf, each.name,each.produce,each.amount
于 2012-06-20T02:14:19.333 に答える
0

CSVライターを使うだけ

add below code:
csvWriter = csv.writer(open('csvFileSource.csv', 'wb'), delimiter=',', quotechar='|',quoting=csv.QUOTE_MINIMAL)
      csvWriter.writerow(each.name + each.produce + each.amount) 

完全なコード:

import csv

file1  = open('csvFileSource.csv',"r") #Open CSV File in Read Mode
reader = csv.reader(file1)      #Create reader object which iterates over lines

class Object:                   #Object to store unique data
    def __init__(self, name, produce, amount):
        self.name = name
        self.produce = produce
        self.amount = amount

rownum = 0 #Row Number currently iterating over
list = []  #List to store objects

def checkList(name, produce, amount):
    for object in list:  #Iterate through listif object.name == name and object.produce == produce:  #Check if name and produce combination exists
        object.amount += int(amount) #If it does add to amount variable and break out
        return

newObject = Object(name, produce, int(amount)) #Create a new object with new name, produce, and amount
list.append(newObject)  #Add to list and break out

for row in reader:  #Iterate through all the rows
    if rownum == 0:  #Store header row seperately to not get confused
            header = row
    else:
        name = row[0]  #Store name
        produce = row[1]  #Store produce
        amount = row[2]  #Store amount

        if len(list) == 0:  #Default case if list = 0
            newObject = Object(name, produce, int(amount))
            list.append(newObject)
        else:  #If not...
            checkList(name, produce, amount)


rownum += 1
csvWriter = csv.writer(open('csvFileDestination.csv', 'wb'), delimiter=',', quotechar='|',quoting=csv.QUOTE_MINIMAL)
for each in list:
    csvWriter.writerow(each.name + each.produce + each.amount)
于 2012-06-20T02:25:13.940 に答える