Python Forum
How can I copy and paste data from text file into an Excel sheet?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I copy and paste data from text file into an Excel sheet?
#15
This will read all into a bunch of lists making them very easy to work with:
import os
import re


# Make sure path is set to current directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))


def read_data(filename):
    with open(filename) as fp:
        lines = fp.read().split('\n')
        for n, line in enumerate(lines):
            line = line.strip()
            line = re.sub(' +',' ', line)
            # header special case
            if n == 0:
                columns = line.split('\t')
            else:
                columns = line.split()
            print(f"{columns}")


if __name__ == '__main__':
    read_data('example_linearity.txt')
results:
Output:
['Freq(Hz)', 'Class 1 + (dB)', 'Class 1 - (dB)', 'Class 2 + (dB)', 'Class 2 - (dB)', 'voltage (mVrms)', 'NI voltage (mVrms)', 'SLM (dB)', 'NI SLM (dB)', 'Input (dB)', 'Expected SPL (dB', 'overload'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '76.0000', '0.0000', '94.0000', '94.3497', '94.0000', '94.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '135.1492', '0.0000', '99.0000', '99.3496', '99.0000', '99.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '240.3331', '0.0000', '104.0000', '104.3490', '104.0000', '104.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '427.3794', '0.0000', '109.0000', '109.3531', '109.0000', '109.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '760.0000', '0.0000', '114.0000', '114.3511', '114.0000', '114.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '852.7340', '0.0000', '115.0000', '115.1273', '115.0000', '115.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '956.7833', '0.0000', '116.0000', '116.3524', '116.0000', '116.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '1073.5285', '0.0000', '117.0000', '117.3523', '117.0000', '117.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '1204.5188', '0.0000', '118.0000', '118.3528', '118.0000', '118.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '42.7379', '0.0000', '89.0000', '89.3501', '89.0000', '89.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '24.0333', '0.0000', '84.0000', '84.3497', '84.0000', '84.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '13.5149', '0.0000', '79.0000', '79.3494', '79.0000', '79.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '7.6000', '0.0000', '74.0000', '74.3495', '74.0000', '74.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '4.2738', '0.0000', '69.0000', '69.3492', '69.0000', '69.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '2.4033', '0.0000', '64.0000', '64.3489', '64.0000', '64.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '1.3515', '0.0000', '59.0000', '59.3489', '59.0000', '59.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.7600', '0.0000', '54.0000', '54.3481', '54.0000', '54.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.4274', '0.0000', '49.1000', '49.3496', '49.0000', '49.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.2403', '0.0000', '44.1000', '44.3487', '44.0000', '44.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.1351', '0.0000', '39.1000', '39.3472', '39.0000', '39.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0760', '0.0000', '34.3000', '34.3524', '34.0000', '34.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0760', '0.0000', '34.3000', '34.3504', '34.0000', '34.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0677', '0.0000', '33.3000', '33.3623', '33.0000', '33.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0604', '0.0000', '32.4000', '32.3638', '32.0000', '32.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0538', '0.0000', '31.5000', '31.3587', '31.0000', '31.0000', '0.0000'] ['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0480', '0.0000', '30.5000', '30.3642', '30.0000', '30.0000', '1.0000']
Reply


Messages In This Thread
RE: How can I copy and paste data from text file into an Excel sheet? - by Larz60+ - Mar-19-2020, 07:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 287 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Python openyxl not updating Excel file MrBean12 1 342 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  What script to paste folders thenewcoder 1 677 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,265 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 854 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  How to copy work sheet data one workbook to other? sayyedkamran 2 711 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,913 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,071 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,116 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020