Is there a pythonic way of reading - say - mixed integer and char input without reading the whole input at once and without worrying about linebreaks? For example I have a file with whitespace-separated data of which I only know that there are x integers, then y chars and then z more integers. I don't want to assume anything about linebreaks.
I mean something as mindless as the following in C++:
...
int i, buf;
char cbuf;
vector<int> X, Z;
vector<int> Y;
for (i = 0; i < x; i++) {
cin >> buf;
X.push_back(buf);
}
for (i = 0; i < y; i++) {
cin >> cbuf;
Y.push_back(cbuf);
}
for (i = 0; i < z; i++) {
cin >> buf;
Z.push_back(buf);
}
EDIT: i forgot to say that I'd like it to behave well under live input from console as well - i.e. there should be no need to press ctrl+d before getting tokens and the function should be able to return them as soon as a line has been entered. :)
How about a small generator function that returns a stream of tokens and behaves like cin:
def read_tokens(f):
for line in f:
for token in line.split():
yield token
x = y = z = 5 # for simplicity: 5 ints, 5 char tokens, 5 ints
f = open('data.txt', 'r')
tokens = read_tokens(f)
X = []
for i in xrange(x):
X.append(int(tokens.next()))
Y = []
for i in xrange(y):
Y.append(tokens.next())
Z = []
for i in xrange(z):
Z.append(int(tokens.next()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With