0

I'm trying to read a file in io but it returns an error. It's giving me a TypeError which I'm not able to solve. This is the code I'm using:

str_summary = pd.read_sql("SELECT * FROM '" + str(overall_summary) + "'", conn)
s = io.StringIO()
csv.writer(s).writerows(str_summary)
s.seek(0)
buf = io.BytesIO()
buf.write(s.getvalue().encode())
buf.seek(0)
buf.name = f'data1.csv'
with open(buf) as red:
    csvdata=csv.reader(red,delimiter=",")
    tdata=[]
    for row in csvdata:
        rowdata = []
        BLANK=row[0]
        A1 =row[1]
        A2=row[2]
        B3=row[3]
        B4=row[4]
        B5=row[5]
        C6=row[6]
        C7=row[7]
        C8=row[8]

It returns this error

TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO
1
  • @Jan. To me it is not that's why I brought it here. Please help answer. Commented Jun 8, 2020 at 20:59

1 Answer 1

1

just don't use open, but use csvdata=csv.reader(buf, delimiter=",")

Sign up to request clarification or add additional context in comments.

2 Comments

Gives me this eror,,, @Ruslan...Traceback (most recent call last): File "D:\Python\PyQt5\Backup\Result Management System(RMS)\REMARE 2.py", line 2098, in get_results for row in csvdata: _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
If you need a file object, then you can use buf = io.FileIO(f'data1.csv','r+')

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.