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
Jan
  • 42,290
  • 8
  • 54
  • 79
fakeMake
  • 738
  • 8
  • 18

1 Answers1

1

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

Ruslan
  • 191
  • 5
  • 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?) – fakeMake Jun 08 '20 at 20:50
  • 1
    If you need a file object, then you can use `buf = io.FileIO(f'data1.csv','r+')` – Ruslan Jun 08 '20 at 21:19