![]() ![]() Joined_output_string = "\n\n".join(findall_matches) Putting all together, as a replacement for this line, you’d have something like: html_input = f.read()įindall_matches = re.findall("(.*)", html_input, flags=re.DOTALL) (Also, FYI, the the `?` in the regex is unnecessary, as `*` already means zero or more matches, but not incorrect). You can resolve this by passing () to `re.findall()`, which makes `.` (dot) match all characters including `\n`. does not match \n (line break), it will not match blocks that have any line breaks anywhere in or between the opening and closing tags. So, you’d have \n\n.join(your_findall_output), which will return a single string with all of the paragraph blocks separated by two line breaks, which you can then pass to f.write().Īlso, your regex will correctly match any elements that have their opening tag, content and closing tag completely on one line, with no line breaks. your_findall_output, assuming you’ve assigned the output of findall to a variable of that name). “\n\n” for two line breaks) as the object you’re calling it on, and the list of strings you want to join into one as the argument (e.g. join() method of strings with the seperator string (e.g. The simplest solution is to just seperate them with linebreaks, and is fine if you don’t need to process them further individually (if you do, you’d want to consider a different character that didn’t appear in the data). You need to decide, based on the parameters of your assignment, if and how you want to separate the strings in your output file. f2.write(your_output_string_here).įurthermore, was also correct in inferring that you need to pass a string to the. write() method with the string you want to write, i.e. write attribute (in this case, a method) of the file. To read the file contents from the file, you need to use f.read() (preferably inside a with block, as above).Īs also mentioned, what you’re doing above assigns the result of regex to the. Here’s where your problems are-this isn’t doing what you probably think it is, for multiple reasons:Īs implied, str(f) does not give you the file contents as a string rather, it gives you information about the file object. *f2.write = re.findall("(.*?)", str(f)) # select all text between html tags and save to txt file* ![]()
0 Comments
Leave a Reply. |