0

Here is the code and I am trying to see what I have done wrong. I am new to python functions and linking external files so it would be nice if you could explain your code.

def get_data(filename):

  records = []
 
  with open(filename) as readfile:
    lines = readfile.readlines()
    for line in lines:
      # variable line contains: 
      str_rec = line.split(",") 
      pname = str_rec[0]
      price = int(str_rec[1])
      quantity = int(str_rec[2])
      records.append([pname, price, quantity])

  #caution: indentation
  return records


hell= get_data(data.txt)
print(hell)

data.txt is a link to another file that I am trying to pass as an argument.

1
  • 2
    data.txt is an object (here not defined). "data.txt" is a string describing the path to the file to open Commented Jun 7, 2022 at 10:24

1 Answer 1

2

open(filename) takes the filename as a string, so you should pass the name as a string, not the actual file.

hell= get_data("data.txt")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.