Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Hey guys I got a question about dictionaries!

So here is my situation

1. I am given a file with multiple paragraphs of random text.

2. A new paragraph is specified by a empty line or \n of course

3. I need to read through the file and separate each paragraph from each other, and I would like to make a dictionary

4. So I want the key in my dictionary to be the paragraph number

5. Then the value to be a list of each line in that paragraph.

example of output below (by the way '#'s' are comments so I can help explain what I want)

#File 1:

#paragraph 1
'hey my name is joe'
'I am from the US'
'I love to play sports'
\n
\n
#paragraph 2
'what do you like to do?'
'where are you from'
\n
\n


So that is the text file example

This is kind of what I want to do:

d = {}

d ={paragraph1 : [['hey my name is joe'], ['I am from the US'], ['I love to play sports']], paragraph2 : [['what do you like to do?'],['where are you from']]}



But since I wont know how many paragraphs are in the file, nor will I know how many lines are in the paragraph, how can I read the file create a standard key value such as paragraph and every time a new paragraph appears increment it by one....I imagine you will use a loop to do this.
Here you go!
PHP Code

<?php 
def open_file
(filename):
    
with open(filename, 'r') as f:
        return {
'paragraph ' + str(int(key + 1)):paragraph.split('\n') for key, paragraph in enumerate(f.read().split('\n\n'))}

if 
__name__ == '__main__':
    
output = open_file(filename='textfile.txt')
    print(
output) 

Output:
Code
{'paragraph 1': ['test', 'this'], 'paragraph 2': ['enjoy', 'this']}
@Hunter I doubt he wants each word in a list tbh. Definitely on the right track using a simple dict comprehension with enumerate, though. Just don't use paragraph.split() in that fashion.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
(Mar 22, 2017 09:11 AM)Goodies Wrote:  @Hunter I doubt he wants each word in a list tbh. Definitely on the right track using a simple dict comprehension with enumerate, though. Just don't use paragraph.split() in that fashion.

Sorry, my example was a bit off. Picture the text file like this:

Quote:test
this

enjoy
this

I shouldn't have used words instead of sentences, my bad.

For a text file like this:
Quote:hello my name is Jimmy
Nice to meet you

How are you today?
I'm good thank you

this would be the output:
Quote:{'paragraph 1': ['hello my name is Jimmy', 'Nice to meet you'], 'paragraph 2': ['How are you today?', 'I'm good thank you']}
Just like he displayed as his preferred output.

Well, he wanted the output like this:
Quote:{paragraph1 : [['hey my name is joe'], ['I am from the US'], ['I love to play sports']], paragraph2 : [['what do you like to do?'],['where are you from']]}
But then the embedded lists would be redundant due to them only containing one string each.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by twistingtideMar 30, 2017 08:16 AM 5 153 Mar 30, 2017 08:16 AM
Last Post: (╯°□°)╯︵ ┻━━┻
[.NET] by dirty cowJan 20, 2017 01:43 PM 2 134 Jan 20, 2017 01:43 PM
Last Post: Soulmate~っ☆
[Python] by mr_pedowbearOct 14, 2016 07:12 AM 6 150 Oct 14, 2016 07:12 AM
Last Post: Ensky