Skip to main content

Python: get data from a text the fast way


This is the fastest way to get data from an external txt file that is in the same dir of the .py file with this code.

You simply assign a name to a list that will get the data opening the txt file and splitting the data by the method split(), assuming that each element of the list will be a line of the text.

The text (data.txt) will contain this data

data 1
data 2
data 3
data 4
data 5

The code in the py file (data.py) will read those data with this code:

    >>> with open('data.txt',encoding='UTF-8') as data: 
    >>>     data = data.readlines()
    ...
    >>> print(data)
    ['data 1','data 2','data 3','data 4','data 5']


so, the read() method get the content of the txt file as a strint and the split() methods returns a list from the string in which each element is a line, because of the argument passed '\n' that indicates a new line. In fact, read() returns a string that has '\n' at the end of each line. With split('\n') the string is divided and so each line becomes an element of the list.

Comments

Post a Comment

Popular posts from this blog

Widgets for Jupyter Notebook: a text input widget

Widgets for Jupyter notebook ¶ Let's import the module ipywidgets into the Jupyter Notebook from ipywidgets import widgets from ipywidgets import * from traitlets import * Now we import the display function from IPython ¶ let's attach a function to the event on_submit After we run this cell, we can go up and write something in the text widget and after you submit the text you wrote will be printed after the cell from IPython.display import display text = widgets . Text () display ( text ) def handle_submit ( sender ): print ( "Thank you for entering this text:" , text . value ) text . on_submit ( handle_submit ) Thank you for entering this text: Ciao

Image in Jupyter and PIL step by step

Hi, """ Hi, we will see a step by step tutorial about PIL and IPython.core.display modules to create images from other images and diplaying them in Jupyter notebook """ # What we will do # Create a card # 1. Take a pic of a heart # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle # 4. show the card """ As first step wi will simply display an image on the notebook. I will show two way to display the image with 'display' from IPhyton a. Using the open method of PIL.Image (named Img) b. Using the Image method from the IPython.core.display module """ # 1. Take the pic of a heart from IPython.core.display import Image , display from PIL import Image as Img heart = 'img/heart.png' display ( Image ( heart )) display ( Img . open ( heart )) # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle #...

Let's draw a circle with PIL in Python

Let's continue making our coding around PIL. Let's start with some basic drawing: a circle from PIL import Image , ImageDraw img = Image . new ( "RGB" ,( 60 , 60 ), 'white' ) dr = ImageDraw . Draw ( img ) dr . ellipse (( 0 , 0 , 60 , 60 ), 'yellow' ) img . show () this is the image produced *If you use jupyter notebook, just write img at the end to see the output.