title: TypeError: a bytes-like object is required, not 'str' py 2.7 to py 3.6
body: <p>I am a Python learner and new for stackoverflow. The following code was written in Python 2.7 and when I try to run it using Python 3.6, I stack with the below error. I read many previous posts on that error but I remain unable to solve my code. kindly indicate to me which line/s need to fix and how.</p>
  
  <pre><code>TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-52-db1423a8bf7b&gt; in &lt;module&gt;
       71 
       72 if __name__ == "__main__":
  ---&gt; 73     main()
  
  &lt;ipython-input-52-db1423a8bf7b&gt; in main()
       54     csvWriter = csv.writer(csvOutput, quoting = csv.QUOTE_NONNUMERIC)
       55 
  ---&gt; 56     csvWriter.writerow(["Ticker", "DocIndex","IndexLink", "Description", "FilingDate","NewFilingDate"])
       57     csvOutput.close()
       58 
  
  TypeError: a bytes-like object is required, not 'str'
  </code></pre>
  
  <pre><code>import os,sys,csv,time # "time" helps to break for the url visiting 
  from bs4 import BeautifulSoup   # Need to install this package manually using pip
                                  # We only import part of the Beautifulsoup4
  import urllib.request
  from urllib.request import urlopen
  
  os.chdir('E:\Python\python_exercise') # The location of your file "LongCompanyList.csv"
  companyListFile = "CompanyList.csv" # a csv file with the list of company ticker symbols and names (the file has a line with headers)
  IndexLinksFile = "IndexLinks.csv" # a csv file (output of the current script) with the list of index links for each firm (the file has a line with headers)
  
  def getIndexLink(tickerCode,FormType):
      csvOutput = open(IndexLinksFile,"a+b") # "a+b" indicates that we are adding lines rather than replacing lines
      csvWriter = csv.writer(csvOutput, quoting = csv.QUOTE_NONNUMERIC)
  
      urlLink = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&amp;CIK="+tickerCode+"&amp;type="+FormType+"&amp;dateb=&amp;owner=exclude&amp;count=100"
      pageRequest = urllib.Request(urlLink)
      pageOpen = urllib.urlopen(pageRequest)
      pageRead = pageOpen.read()
  
      soup = BeautifulSoup(pageRead,"html.parser")
  
      #Check if there is a table to extract / code exists in edgar database
      try:
          table = soup.find("table", { "class" : "tableFile2" })
      except:
          print ("No tables found or no matching ticker symbol for ticker symbol for"+tickerCode)
          return -1
  
      docIndex = 1
      for row in table.findAll("tr"):
          cells = row.findAll("td")
          if len(cells)==5:
              if cells[0].text.strip() == FormType:
                  link = cells[1].find("a",{"id": "documentsbutton"})
                  docLink = "https://www.sec.gov"+link['href']
                  description = cells[2].text.encode('utf8').strip() #strip take care of the space in the beginning and the end
                  filingDate = cells[3].text.encode('utf8').strip()
                  newfilingDate = filingDate.replace("-","_")  ### &lt;=== Change date format from 2012-1-1 to 2012_1_1 so it can be used as part of 10-K file names
                  csvWriter.writerow([tickerCode, docIndex, docLink, description, filingDate,newfilingDate])
                  docIndex = docIndex + 1
      csvOutput.close()
  
  
  def main():  
      FormType = "10-K"   ### &lt;=== Type your document type here
      nbDocPause = 10 ### &lt;=== Type your number of documents to download in one batch
      nbSecPause = 0 ### &lt;=== Type your pausing time in seconds between each batch
  
      csvFile = open(companyListFile,"r") #&lt;===open and read from a csv file with the list of company ticker symbols (the file has a line with headers)
      csvReader = csv.reader(csvFile,delimiter=",")
      csvData = list(csvReader)
  
      csvOutput = open(IndexLinksFile,"a+b") #&lt;===open and write to a csv file which will include the list of index links. New rows will be appended.
      csvWriter = csv.writer(csvOutput, quoting = csv.QUOTE_NONNUMERIC)
  
      csvWriter.writerow(["Ticker", "DocIndex","IndexLink", "Description", "FilingDate","NewFilingDate"])
      csvOutput.close()
  
      i = 1
      for rowData in csvData[1:]:
          ticker = rowData[0]
          getIndexLink(ticker,FormType)
          if i%nbDocPause == 0:
              print (i)
              print ("Pause for "+str(nbSecPause)+" second .... ")
              time.sleep(float(nbSecPause))
          i=i+1
  
      csvFile.close()
      print ("done!")
  
  if __name__ == "__main__":
      main()
  </code></pre>