Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open file in text mode: open(<file>, 'rt') #22

Open
gregpalmr opened this issue Nov 30, 2021 · 1 comment
Open

Open file in text mode: open(<file>, 'rt') #22

gregpalmr opened this issue Nov 30, 2021 · 1 comment

Comments

@gregpalmr
Copy link

Currently the open() method opens the file in "byte" mode which causes the following error, when you try to run an iterator over the file contents:

 Error: iterator should return strings, not bytes (did you open the file in text mode?)

It would be very useful to allow the caller to specify that the file be open in "text" mode so that the following code would work without error:

import sys
import alluxio
from alluxio import option
import csv

with alluxio_client.open('/tmp/customers.csv', 'rt') as alluxio_file:
    csvreader = csv.reader(alluxio_file, delimiter=',', quotechar='|')
    for row in csvreader:
        print (', '.join(row))
@psolomin
Copy link
Contributor

psolomin commented Jun 4, 2022

@gregpalmr will this approach work for your case?

import codecs
import csv

from alluxio import Client

c = Client(host=..., port=39999)

with c.open("/test.csv", "w") as f:
   f.write("1,2\n")

with c.open("/test.csv", "r") as alluxio_file:
    rdr = csv.reader(codecs.iterdecode(alluxio_file, 'utf-8'), delimiter=',')
    for row in rdr:
        print(', '.join(row))

# prints out 1, 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants