• Register
1 vote
3.2k views

Problem :

I am very new to Python. Currently I have one textfile and now I am trying to load it and then want to make the list which should contain each and every line from the text file. I have already tried to use the split() procedure. But I am facing below error.

Attributeerror: '_io.textiowrapper' object has no attribute 'split'”.

As I am very new to Python so I am unable to understand above error and I have already wasted a lot of time in understanding and finding workaround for above error.So I am looking for the quick fix on above error.

8 5 2
3,230 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution :

I can definitely help you in finding a quick fix on your error.I think you are trying to use the str methods on the open file object. So you can read the required text file as the list of lines by very easily calling list() on your file object as shown below for your reference:

with open('mylines.txt') as f:
    mylist = list(f)

Please note: This list will also include the newline characters. To strip those in a list comprehension you can use below code:

with open('mylines.txt') as f:
    mylist = [line.rstrip('\n') for line in f]
I hope above solution will help you in getting a quick fix for your error.
8 4
5,680 points
0 votes

Solution:

You are employing str systems on an open file object.

You can read the file as a list of lines by easily calling list() on the file object:

with open('goodlines.txt') as f:
    mylist = list(f)

This does add the newline characters. You can slice those in a list contact:

with open('goodlines.txt') as f:
    mylist = [line.rstrip('\n') for line in f]

Attempt this:

 >>> f = open('goodlines.txt')
 >>> mylist = f.readlines()

open() function returns a file object. And for file object, there is no system like splitlines() or split(). You could use dir(f) to view all the methods of file object.

You're not reading the file content:

my_file_contents = f.read()

 Without calling read() or readlines() loop over your file object:

f = open('goodlines.txt')
for line in f:
    print(line)

In case you want a list out of it (without \n as you asked)

my_list = [line.rstrip('\n') for line in f]

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 23 views
23 views
Problem: attributeerror: 'series' object has no attribute 'reshape'
asked Feb 18 charles mathews 3.8k points
0 votes
1 answer 29 views
29 views
Problem: I want to count the number of times a word is being repeated in the review string I am reading the csv file and storing it in a python dataframe using the below line reviews = pd.read_csv("amazon_baby.csv") The code in the below lines work when I apply it to a single review. print reviews["review"][1] a = reviews["review"][1].split("disappointed") print a b = len(a) print b
asked Feb 23 Mashhoodch 10.1k points
0 votes
1 answer 237 views
237 views
Problem: I have only basic knowledge in python and urllib. Currently I am trying to write the program which should download the mp3's from the website and after that join them together but on every occasion I try to download the mp3 files I face below error: Traceback ( ... usc.edu/~chiso/oldspice/m-b1-hello.mp3") Looking for the python and urlllib expert who can help me in fixing my above error.
asked Jul 22, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 476 views
476 views
When trying to find the index of v but it gave me: 'numpy.ndarray' object has no attribute 'index' none of them have answered my question v = np.random.randn(10) print(v) maximum = np.max(v) minimum = np.min(v) print(maximum, minimum) v.index(maximum, minimum)
asked Oct 30, 2020 psandprop 2.4k points
0 votes
1 answer 335 views
335 views
Python 3.3.2 import json & urllib.request Json [{"link":"","orderid":"100000222"}, {"link":"","orderid":"100000222"}, {"link":"","orderid":"100000222"}] print(response.info()) Date: Sun, 20 Oct 2013 07:06:51 GMT Server: Apache X-Powered-By: PHP/5.4.12 Content ... line 271, in load return loads(fp.read(), AttributeError: 'str' object has no attribute 'read' >>> . Any idea how i can fix this issue?
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 173 views
173 views
This line: sift = cv2.xfeatures2d.SIFT_create() return error: Traceback (most recent call last): File "C:/Python27/openCVskrypty/GUI/SOLUTION2.py", line 11, in <module> sift = cv2.xfeatures2d.SIFT_create() AttributeError: 'module' object has no attribute 'xfeatures2d' I ... ) and I haven't got xfeatures2d module. Does anyone know why? Can I download it separately? Thanks for help how fix this.
asked Oct 24, 2020 psandprop 2.4k points
0 votes
1 answer 107 views
107 views
Using Python Regex to retrieve certain content from a search box on a webpage with selenium.webDriver searchbox = driver.find_element_by_class_name("searchbox") searchbox_result = re.match(r"^.*(?=(\())", searchbox).group() The code works as long as the search box ... is error: AttributeError: 'NoneType' object has no attribute 'group' How can I make the script handle the "No results" situation?
asked Oct 24, 2020 psandprop 2.4k points