-1

Recently, I started to create a program that I could write and record work that I had to do, in order to maintain control of the work that I had done, and work that still needs to be completed. However, I have one large issue at the moment, and it is that the program can read the data that has been sent to the text file where everything is recorded, but cannot use the '.split'function.

The code for this part of the program is the following:

def dialog3():
    document = open('homework.txt','r+')
    doc2 = document.readlines()
    print(doc2)
    doc3 = doc2.split(['\n'])
    btn.configure(state=DISABLED)
    btn2.configure(state=DISABLED)
    btn3.configure(state=DISABLED)

Along with the following error message:

    doc3 = doc2.split(['\n'])
    AttributeError: 'list' object has no attribute 'split'
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • 4
    `doc2` is not a single line but a list of all the lines in `document`. You should use some for loop to look through each of the lines and then use split. Something like `for line in doc2: doc3 = line.split(['\n'])` – Sheldore Sep 29 '18 at 16:25
  • list objects has not split method, just string has split method – felipsmartins Sep 29 '18 at 16:25
  • file[`.readlines`](https://docs.python.org/3/library/io.html#io.IOBase.readlines) returns a list of lines (strings). – wwii Sep 29 '18 at 16:28
  • Possible duplicate of [How do I split a multi-line string into multiple lines?](https://stackoverflow.com/questions/172439/how-do-i-split-a-multi-line-string-into-multiple-lines) – shad0w_wa1k3r Sep 29 '18 at 16:28
  • Possible duplicate of [Attribute Error: 'list' object has no attribute 'split'](https://stackoverflow.com/questions/30042334/attribute-error-list-object-has-no-attribute-split) – stovfl Sep 29 '18 at 16:37
  • Possible duplicate of [Attribute Error: 'list' object has no attribute 'split'](https://stackoverflow.com/questions/30042334/attribute-error-list-object-has-no-attribute-split) ... Found with search terms: `python split readlines AttributeError: 'list' object has no attribute 'split'` – wwii Sep 29 '18 at 16:37

3 Answers3

1

Lets say you have a file which looks like-

abc def ged

adw ret qwer tre....

so on

Now when I do readlines the cursor pointing to the file object will be on the first line..and If I do file.readlines.split(' ') will return a list of words in the first line...readline.split('\n') will return the first line itself.

Since each line is already split in newlines it will return the same line

ubuntu_noob
  • 2,305
  • 6
  • 23
  • 64
  • @FlyingTeller thanks for the catch!...meant with the split – ubuntu_noob Sep 29 '18 at 17:02
  • You probably meant `file.readline().split(' ')`. using `readlines` without `()` will throw an error and doing `file.readlines().split(' ')` is basically what OP is trying, which is also not correct – FlyingTeller Sep 29 '18 at 17:04
1
doc2 = document.readlines()
print(doc2)
doc3 = doc2.split(['\n'])

The type() of doc2 would already be an array because file.readlines() always return an array of lines within the file. You cannot simply split an array. From the look of your problem, your file may potentially have \n escape characters so this would be captured also within the arrays.

0

the readlines() function returns a list, so you don't need to split it into lines again.

if you needed to split it further you could have done something like

flatten = lambda l: [item for sublist in l for item in sublist]
doc3=flatten([l.split('\n') for l in doc2])
Arnon Rotem-Gal-Oz
  • 25,469
  • 3
  • 45
  • 68