Skip to main content
Chemistry LibreTexts

5.2.4: How to Incorporate Data Files in Your Code Blocks- For Python

  • Page ID
    215860
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    There are a number of ways data files and can be incorporated into code blocks. However, ensure that you have already followed the steps in the introduction to the CKEditor Binder Plugin Tutorial here.

    Method 1: Using wget

    The first method that can be used to get a data file in your code block is with wget. Follow the example below and replace the example URL with the one attained in the previous step.

    !wget https://chem.libretexts.org/@api/deki/files/265970/auto-mpg.data

    Remember to press the "Run" button prior to pressing "OK". Printing both the code and output without, will the below result, to avoid this simply check "Insert Code Only" after pressing "Run".

    !wget https://chem.libretexts.org/@api/deki/files/265970/auto-mpg.data
    --2020-05-18 01:28:43--  https://chem.libretexts.org/@api/deki/files/265970/auto-mpg.data
    Resolving chem.libretexts.org (chem.libretexts.org)... 143.204.39.66, 143.204.39.114, 143.204.39.103, ...
    Connecting to chem.libretexts.org (chem.libretexts.org)|143.204.39.66|:443... connected.
    HTTP request sent, awaiting response... 302 Moved Temporarily
    Location: https://files.mtstatic.com/site_4334/265970/0?Expires=1589768697&Signature=c0dsv~bR3RQniUY5r-nXlduGKS~Az6ThF-JmogVIJG2CWYV0oDujk8cDVGicmlVbR-uverfdaQGQk~RSe2MqxK8CtJ8Io4Jc2YyAFg2WmjzPbWNWkpiUIzuxJvqJ~-LnVjmIvjDVL5YWRBW1BhoEd59mNp0q9EHx1Mzbh7o36a4_&Key-Pair-Id=APKAJ5Y6AV4GI7A555NA [following]
    --2020-05-18 01:28:43--  https://files.mtstatic.com/site_4334/265970/0?Expires=1589768697&Signature=c0dsv~bR3RQniUY5r-nXlduGKS~Az6ThF-JmogVIJG2CWYV0oDujk8cDVGicmlVbR-uverfdaQGQk~RSe2MqxK8CtJ8Io4Jc2YyAFg2WmjzPbWNWkpiUIzuxJvqJ~-LnVjmIvjDVL5YWRBW1BhoEd59mNp0q9EHx1Mzbh7o36a4_&Key-Pair-Id=APKAJ5Y6AV4GI7A555NA
    Resolving files.mtstatic.com (files.mtstatic.com)... 52.85.79.99, 52.85.79.82, 52.85.79.53, ...
    Connecting to files.mtstatic.com (files.mtstatic.com)|52.85.79.99|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 30286 (30K) [application/octet-stream]
    Saving to: ‘auto-mpg.data.1’
    
    auto-mpg.data.1     100%[===================>]  29.58K  --.-KB/s    in 0.04s   
    
    2020-05-18 01:28:43 (708 KB/s) - ‘auto-mpg.data.1’ saved [30286/30286]
    
    

    Once the data file has been loaded, use Python code that you may normally use to read out the lines in the data file. An example code block has been provided below.

    inFile = open("auto-mpg.data", "r")
    
    line = inFile.readline()
    
    while True:
      line = inFile.readline()
      if line == "":
        break
      line = line.strip()
      lineList = line.split(',')
      print(lineList)

    To ensure that the above code block runs successfully, the user needs to run the wget code prior to running the above code if both parts are added in separately.

    Method 2: Using Python Libraries

    The CKEditor Binder Plugin supports Python 3, libraries that are supported by this version of Python may be used to read in the file. An example code block is provided below.

    import csv
    import urllib.request
    import codecs
    
    url = "https://chem.libretexts.org/@api/deki/files/265970/auto-mpg.data"
    ftpstream = urllib.request.urlopen(url)
    csvfile = csv.reader(codecs.iterdecode(ftpstream, 'utf-8'))
    for line in csvfile:
      print(line)

    5.2.4: How to Incorporate Data Files in Your Code Blocks- For Python is shared under a CC BY 1.3 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?