Search code examples
pythonhistogramdata-analysis

Plotting in Python


I'm new to python and was curious as to how, given a large set of data consisting of census information, I could plot a histogram or graph of some sort. My main question is how to access the file, not exactly how the graph should be coded. Do I import the file directly? How do I extract the data from the file? How is this done?

Thanks


Solution

  • You cannot directly import a data file in a python script. You need to open the file for reading and then parse it according to the format of data stored in the file.

    For reference, Here is an example of how to read a text file:

    # To read all data at once
    with open("/path/to/file.txt") as file_handle:
        file_contents = file_handle.read()
    
    # To read one line at a time
    with open("/path/to/file.txt") as file_handle:
        for line in file_handle:
            line = line.strip()
            # Do more stuff with line