Python - The '.split' function is not working correctly

python tutorial
python download
what is python used for
python documentation
python interpreter
python -m syntax
python documentation pdf
python wiki

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()
    doc3 = doc2.split(['\n'])

Along with the following error message:

    doc3 = doc2.split(['\n'])
    AttributeError: 'list' object has no attribute 'split'

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

The Python Tutorial, Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data​  Learn Python Like a Pro. From The Basics All The Way to Creating your own Apps and Games! Join Over 50 Million Students Already Learning Online With Udemy

doc2 = document.readlines()
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.

General Python FAQ, An opinionated guide to the Python programming language and a best practice handbook to the installation, configuration, and usage of Python on a daily basis. Get Where To Learn Python. Find Instant Quality Info Now!

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])

The Hitchhiker's Guide to Python, Beginner¶. The Python Tutorial¶. This is the official tutorial. It covers all the basics​, and offers a tour of the language and the  Enhance Your Python Skills With Expert-Led Online Video Tutorials - Start Now!

Learning Python, Netanyahu's New Government: Monty Python, the Purge and Goodfellas Combined. Israel's newly-appointed ministers have already delivered on  Python was created in the early 1990s by Guido van Rossum at Stichting Mathematisch Centrum in the Netherlands as a successor of a language called ABC. Guido remains Python’s principal author, although it includes many contributions from others.

Monty Python, Zed Shaw has perfected the world's best system for learning Python. Follow it and you will succeed-just like the hundreds of thousands of beginners Zed has  In Greek mythology, Python was the serpent, sometimes represented as a medieval-style dragon, living at the center of the earth, believed by the ancient Greeks to be at Delphi.

Python (programming language), This Python book Includes 3 Books: Python Programming For Beginners - Learn The Basics Of Python In 7 Days! Calculations are simple with Python, and expression syntax is straightforward: the operators +, -, * and / work as expected; parentheses can be used for grouping. More about simple math functions in Python 3 .

  • 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'])
  • list objects has not split method, just string has split method
  • file.readlines returns a list of lines (strings).
  • Possible duplicate of How do I split a multi-line string into multiple lines?
  • Possible duplicate of Attribute Error: 'list' object has no attribute 'split'
  • @FlyingTeller thanks for the catch!...meant with the split
  • 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