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
    

    这使用 shell 重定向将标准输出保存到您指定的任何文件。这非常灵活并且很容易构建到未来的脚本中。如果您接受标准输入或通过命名文件的输入,那就特别棒了,它使您的工具非常灵活。

  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 回答