Typeerror: split() Missing 1 required positional argument: 'string'

Asked

Viewed 765 times

1

One more basic question on my part. I am 'Pythonist' super basic level and I am simply trying to read a text line by line and make a split each time I find a punctuation mark or a space.

I tried that:

import re

with open('est.txt', 'r') as f: 
for ligne in f: 
    ligne=re.split('; |, |\s|\.|\n') 
    print (ligne)

Only I have the following mistake:

Traceback (most recent call last):
File "C:/Users/Janaina/Desktop/exo/stats_python/estpy.py", line 5, in <module> ligne=re.split('; |, |\s|\n')
TypeError: split() missing 1 required positional argument: 'string'

Could someone help me? I know it’s something super basic, but I’ve been blocked for some time looking for the answer...

2 answers

2


The function re.split must receive at least two arguments, regular expression and string where you will apply it. You are reporting only regular expression.

Maybe that’s what you want to do:

import re

with open('est.txt', 'r') as f: 
    for ligne in f: 
        ligne = re.split('; |, |\s|\.|\n', ligne) 
        print (ligne)

0

By the way, always put the r of raw mode -- that is to say r'expressãoregular' -- for example '\s' r'\s' are different things...

import re

with open('est.txt', 'r') as f: 
    for ligne in f:                           # para cada linha
        frases = re.split(r'[;,.!]\s', ligne  # split por pont. espaço
        print(frases)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.