title: Convert .txt to .csv with AWS Lambda function
body: <p>I am trying to create a Lambda function which is triggered whenever a .txt file is dropped in the bucket 'testinput' (a private bucket), convert it from a .txt file (with a pipe delimiter) to a .csv file, and then put that converted file into the bucket 'testoutput' (a private bucket).</p>
  <p>I created a Lambda function using the code below and set up the S3 trigger. I followed the steps in this <a href="https://medium.com/@melissa_89553/how-to-import-python-packages-in-aws-lambda-pandas-scipy-numpy-bb2c98c974e9" rel="nofollow noreferrer">blog</a> so that I could import Pandas. However, when I drop a file 'pipedelimitedtest' into 'testinput', nothing happened.</p>
  <p>This leads me to believe that there is something wrong with the code I am using for converting the .txt file to a .csv file.</p>
  <p>If someone could provide insight into what specifically is wrong with my code, or if there is a simpler way to address this problem, I would greatly appreciate it.</p>
  <p>UPDATE: I changed the script based on the helpful feedback from Marcin and added a pandas layer to the function. However, when I test the function, I am now getting the error: Unable to import module 'lambda_function': No module named 'pandas'.</p>
  <pre><code>import boto3
  import io
  import pandas as pd
  import json
  
  def lambda_handler(event, context):
      s3 = boto3.client('s3')
      obj = s3.get_object(Bucket='lambdatestinput985', Key='Pipedelimitedtest.txt')
      df = pd.read_csv(io.BytesIO(obj['Body'].read()))
  
      csv_buffer = StringIO()
      df.to_csv (csv_buffer)
      
      s3_resource = boto3.resource('s3')
      s3_resource.Object(Bucket, 'df.csv').put(Body = csv_buffer.getvalue())
  
      return {
             'statusCode': 200,
             'body': json.dumps('Hello from S3 events Lambda!')
         }
  </code></pre>