简体   繁体   中英

AttributeError: 'str' object has no attribute 'csv'

I am trying to write a CSV file and I have code to create a document with a header file this code will take inputs to write to that same file.

class CSVFile:

    def __init__(self, doctitle):
        #creates the physical doc on the disk
        #creates the header row in the .csv file
        self.doctitle = doctitle
        self.f = open(doctitle + ".csv", 'w+')
        self.f.write("vianumber, innerdiameter, outerdiamter, ratio \n")
        self.closedoc()
        return


    def appendrow(self, doctitle, vianumber, innerdiameter, outerdiamter, ratio):
        #called for each measured via
        self.f = open(doctitle + ".csv", 'a+')
        self.f.write(vianumber, innerdiameter, outerdiamter, ratio)
        self.closedoc()
        return

    def closedoc(self):
        #filize the document
        self.f.close()
        return

The error message I get is the following:

 CSVFile.appendrow("", "test", 2, 3, 4, 5)
Traceback (most recent call last):

  File "<ipython-input-21-07d259b7d2fa>", line 1, in <module>
    CSVFile.appendrow("", "test", 2, 3, 4, 5)

  File "C:/Users/Brook/Desktop/Senior Design/CSV file script.py", line 23, in appendrow
    self.f = open(doctitle + ".csv", 'a+')

AttributeError: 'str' object has no attribute 'f'

This is because you are not instantiating an object. Your call is CSVFile.appendrow("", "test", 2, 3, 4, 5) . Essentially, it means that for the self parameter of appendrow you are passing an empty string argument "" .

Try something along lines of CSVFile("test").appendrow("test", 2, 3, 4, 5)

You have an error in the self.f.write call in your code as well, but I will let you fix that.

Your class and the way you use it have a lot of issues, such as:

  • You do not use the stored file name.
  • You do not use the standard CSV writer.
  • You do not use with blocks.
  • You do not create a class object.
  • You create unnecessary object attributes.
  • You pass the self parameter to an object method.

Here is an improved version of your code.

import csv

class CSVFile:

    def __init__(self, doctitle):
        self.doctitle = doctitle + ".csv"
        with open(doctitle, 'w+') as f:
            writer = csv.writer(f)
            writer.writerow(["vianumber", "innerdiameter",
                             "outerdiamter", "ratio"])

    def appendrow(self, vianumber, innerdiameter, outerdiamter, ratio):
        #called for each measured via
        with open(self.doctitle, 'a+') as f:
            writer = csv.writer(f)
            writer.writerow([vianumber, innerdiameter, outerdiamter, ratio])

    #def closedoc(self): -- Not needed!
        #filize the document

mycsv = CSVFile("foo")
mycsv.appendrow(2,3,4,5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM