Failing at python
1
Name:
Anonymous
2010-01-27 7:24
All I want to do is read all the lines from a file and put them into a list. This is all. I am failing rather hard. Halp? I'm trying to read each line with a for loop and put them into a list but I can't get it to work.
2
Name:
Anonymous
2010-01-27 7:25
Read SICP. The forced indentation of code. Thread over. My other car is a cdr. Hax my anus
3
Name:
Anonymous
2010-01-27 7:52
(define (file->list file)
(call-with-input-file file
(lambda (in)
(let loop ((collect (list)))
(let ((line (read-line in)))
(if (eof-object? line)
(reverse collect)
(loop (cons line collect)))))))
4
Name:
Anonymous
2010-01-27 8:19
5
Name:
Anonymous
2010-01-27 8:47
>>4
I know what the things are but I'm not sure how to implement it.
6
Name:
Anonymous
2010-01-27 9:24
ITT: /prouge/ codes for you
7
Name:
Anonymous
2010-01-27 9:27
Try a real scripting language:
<?php
$list = file($filename, FILE_IGNORE_NEW_LINES);
?>
8
Name:
Anonymous
2010-01-27 9:31
>>5
Yes, because you're trying to program in Python, but 'implementing' a single function call is beyond your capability. I'll believe that.
9
Name:
Anonymous
2010-01-27 9:45
>>5
Oh, you really need it at all written for you?
file = open('file.txt', 'read').read()
lines = file.readlines().splitlines()
10
Name:
Anonymous
2010-01-27 10:03
>>9
Did you mean
lines = open('file.txt','r').readlines()
11
Name:
Anonymous
2010-01-27 10:09
Use Perl with Tie::File.
12
Name:
Anonymous
2010-01-27 10:22
>>9
I just keep getting errors like 'AttributeError: 'str' object has no attribute 'readlines' for some reason
13
Name:
Anonymous
2010-01-27 10:50
in perl:
my(@lines)=<>;
14
Name:
Anonymous
2010-01-27 13:06
>>13
What's with all the extra characters?
Did you mean:
@l=<>
15
Name:
Anonymous
2010-01-27 13:48
16
Name:
Anonymous
2010-01-27 17:04
Anyone who knows perl should be ashamed of themselves.
17
Name:
Anonymous
2010-01-27 17:54
>>16
use English; before you write anything next time.
18
Name:
Anonymous
2010-01-28 0:45
(defun read-all-strings (file)
(loop
for line = (read-line file nil :eof)
while (not (eql line :eof))
collect line))
19
Name:
Anonymous
2010-11-26 12:11