Python3 AttributeError: module 're' has no attribute 'split' [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am running the code below on my ST3/Atom IDE, it raises the exception that the module 're' has no attribute 'split'. But, when I running the code in the cmd Python script it works well. Can anyone interpret this confusing trouble and give me some advice to make this module works on my IDE? Thanks in advance.
The simple code I tested:
import re
re.split(r'[;,\s]\s', 'hello;,world')

This should work, unless you made the mistake of naming another file in the directory re.py in which case it will look through the re.py file (you created) for split instead of looking through the actual re module when you import re.

instead of the re module you can use the inbuilt features for string variables like:
string = "Some string to be splitted"
splitted_string=string.split(" ") # this will split the string from spaces, change the whitespaces to the characters like comma, colon, period, etc.
mylist = splitted_string
output:
['Some','string','to','be','splitted']
I hoped my answer helped you...

Related

Why is python giving me an error for improper indentation when my indentation is correct? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is the error message I got:
File "main.py", line 15
while True:
^
IndentationError: unindent does not match any outer indentation level
This is my full code:
import wikipedia
from colorama import Fore, Style, Back
y = input("tell me what you want ")
z = int(input("how many sentences "))
try:
text = wikipedia.summary(y, sentences=z)
print('')
print("---Text---")
print(text)
print("----------")
print(len(text.split()),"words.")
except:
print(Fore.RED + "ERROR)
while True:
print("\a")
Can you please explain why this is happening? I am using the Repl online ide.
The answer to this is that I was mixing up tabs and spaces. You shouldn't use both because this error can happen.
While coding in python, it's super important to pay attention to the way you indent. Of course you can either use tab or space, but always make sure you stick with one of them and not mixing them together.
Seems like you haven't done it this time. Delete the indentations and reindent them. Should work fine.

AttributeError: 'tuple' object has no attribute 'readlines' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm new to python environments and I'm currently working on a ml project. While reading a CSV file using readlines function I'm getting "tuple has no attribute readlines". Please someone help me.....
my code is
data_file=(r"C:\Users\Sury teja\temp\mnist_train.csv","r")
print(data_file.readlines())
error is
AttributeError: 'tuple' object has no attribute 'readlines'
You missed open:
data_file = open(r"C:\Users\Sury teja\temp\mnist_train.csv", "r")
print(data_file.readlines())
In the code you wrote, data_file is just a tuple, which looks like this:
('C:\\Users\\Sury teja\\temp\\mnist_train.csv', 'r')
The error here is caused by the parenthesis around two arguments:
(r"C:\Users\Sury teja\temp\mnist_train.csv","r")
This is caused by the fact that parenthesis are serving more than one purpose in Python: they are used for calling - for example - functions, like in this example, but also for initializing immutable data structures called tuples.
By omitting the function name (open) you have initialized a tuple instead of calling the open function. And tuple, like your error suggests, has no attribute readlines.

i don't know how to fix this error, python3 syntax error 'def' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to solve this error.
i tried for almost half hours, but I couldn't find the answer..
This is my error
File "sampling_fun.py", line 71
def average(self) :
^
SyntaxError: invalid syntax
and full code
import csv
class fun :
def __init__(self, rowList, num) :
list = []
listLen = len(self.rowlist)-1
for i in range(listLen) :
list.append(self.rowList[i+1][num] # num = Header 1~4
def average(self) :
ave = sum(self.list)/self.lestLen
print("average : %0.2f" %ave)
return ave
testlist = cssRead('Data_2', 1)
test1 = fun(testlist, 1)
test1.average()
When you encounter such error, check the line before it.
You have a non-matching paranthesis, the closing brace for append is missing.
list.append(self.rowList[i+1][num])
def errors happen when python is not expecting a function definition. As #Siong Thye Goh notes, a missing ) is your issue. For the future, the only time python does not expect a function definition is after an incomplete code-block.
That can happen when you forget to close parentheses or brackets, but also when you forget to put a statement after a colon or don't indent correctly.
Rarely, it can happen due to incompatible space characters when copy-pasting from external sources.

Syntax error for the most basic Python code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have just started learning python and it seems I am getting errors with one line of code
ERROR MESSAGE:
File "hello.py", line 2
print ‘hello world!’
^
SyntaxError: invalid syntax
FULL CODE
# coding: utf-8
print ‘hello world!’
I'm not really sure what could be going wrong in this one simple line
Use single quotes, to be specific ' instead of ’.
Try:
# coding: utf-8
print 'hello world!'
Python accepts either single ' or double " quotes for strings. On special multiline occasions you can use triple quote '''.
Python 3 requires parentheses after print:
print('hello world')

url.strip()+' - CatchAllError' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am try to run this program as it start run but while running it's create error regrading CatchAllError . I want output as ' Pass ' But its not please help. Thanks
if line:
msg = url.strip()+' - CatchAllError'
print msg
with open("log_Error.txt", "a") as log:
log.write(msg+"\n")
else:
pass
You can see my whole program at https://ghostbin.com/paste/ypdmd.
You can see that if condition should be pass but it's not. To include target list go to https://ghostbin.com/paste/pjuox and download target list and other information.
You are mixing tabs and spaces. This confuses Python* and may cause unexpected behavior.
Use only tabs or only spaces, not both. Spaces is preferable.
(*well, not really. Python knows exactly how to handle tabs; it just does so in a way that is very surprising to most users. In particular, one tab is not equivalent to 4 spaces, or 8, or whatever it looks like in your text editor. See 2.1.8 - Indentation for more information.)

Categories