Python Forum
[Solved] Reading every nth line into a column from txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Reading every nth line into a column from txt file
#5
(Jun-28-2021, 01:20 PM)snippsat Wrote: What is the output you want from this?
I can not see why you look for Lifetime,Std deviations...ect in this.
Are you making this file?
When put a Python list ['Fixed', 'Fixed', '0.0339'] in a text file it lose all it's meaning.
Have to parse it back or could done something else like taken out values(eg CSV way) then save a list to a test file.
If you no control of the text file then have to parse it to what you want.

Alright, I must've explained this quite badly, let me try again! The first part of the code (picking Lifetimes etc.) is just sorting out a file (called output) that looks like this:

CA50_40_ref_data2101_E04_spec0-70 #0                                    
             Lifetimes (ns)   :    0.4000    0.1250    2.0446
             Std deviations   :     Fixed     Fixed    0.0339 
             Intensities (%)  :   69.2721    9.6726   21.0553
             Std deviations   :    1.0359    0.8128    0.4063 
Time-zero    Channel number   :   41.5603
             Std deviations   :    0.0588 
CA50_40_ref_data2101_E04_spec0-70 #1                                    
             Lifetimes (ns)   :    0.4000    0.1250    2.0714
             Std deviations   :     Fixed     Fixed    0.0344 
             Intensities (%)  :   70.0338    9.0952   20.8710
             Std deviations   :    1.0308    0.8135    0.4009 
Time-zero    Channel number   :   41.5853
             Std deviations   :    0.0593 
CA50_40_ref_data2101_E04_spec0-70 #2                                    
             Lifetimes (ns)   :    0.4000    0.1250    2.0568
             Std deviations   :     Fixed     Fixed    0.0333 
             Intensities (%)  :   69.5963    8.7445   21.6592
             Std deviations   :    1.0411    0.8177    0.4072 
Time-zero    Channel number   :   41.5541
             Std deviations   :    0.0603 
CA50_40_ref_data2101_E04_spec0-70 #3                                    
             Lifetimes (ns)   :    0.4000    0.1250    2.0321
             Std deviations   :     Fixed     Fixed    0.0329 
             Intensities (%)  :   70.4228    8.0614   21.5158
             Std deviations   :    1.0497    0.8219    0.4105 
Time-zero    Channel number   :   41.4507
             Std deviations   :    0.0604 
CA50_40_ref_data2101_E04_spec0-70 #4                                    
             Lifetimes (ns)   :    0.4000    0.1250    2.0513
             Std deviations   :     Fixed     Fixed    0.0331 
             Intensities (%)  :   67.2025   11.0731   21.7244
             Std deviations   :    1.0204    0.7976    0.4057 
Time-zero    Channel number   :   41.6253
             Std deviations   :    0.0579 
CA50_40_ref_data2101_E04_spec0-70 #5                                  
...
into this (file called out):

#0
0.4000 0.1250 2.0446
['Fixed', 'Fixed', '0.0339']
69.2721 9.6726 21.0553
['1.0359', '0.8128', '0.4063']
41.5603
['0.0588', ' ', ' ']
#1
0.4000 0.1250 2.0714
['Fixed', 'Fixed', '0.0344']
70.0338 9.0952 20.8710
['1.0308', '0.8135', '0.4009']
41.5853
['0.0593', ' ', ' ']
#2
0.4000 0.1250 2.0568
['Fixed', 'Fixed', '0.0333']
69.5963 8.7445 21.6592
['1.0411', '0.8177', '0.4072']
41.5541
['0.0603', ' ', ' ']
#3
0.4000 0.1250 2.0321
['Fixed', 'Fixed', '0.0329']
70.4228 8.0614 21.5158
['1.0497', '0.8219', '0.4105']
41.4507
['0.0604', ' ', ' ']
#4
0.4000 0.1250 2.0513
['Fixed', 'Fixed', '0.0331']
67.2025 11.0731 21.7244
['1.0204', '0.7976', '0.4057']
So the first loop was needed to extract the necessary information from the first file, and now I am trying to get the 'out' file above in this form for easier comparison:

Dataset Lifetimes            Std deviations               Intensities            Std deviations                 Time-zero Std deviation
#0      0.4000 0.1250 2.0446 ['Fixed', 'Fixed', '0.0339'] 69.2721 9.6726 21.0553 ['1.0359', '0.8128', '0.4063'] 41.5603   ['0.0588', ' ', ' ']
#1      0.4000 0.1250 2.0714 ['Fixed', 'Fixed', '0.0344'] 70.0338 9.0952 20.8710 ['1.0308', '0.8135', '0.4009'] 41.5853   ['0.0593', ' ', ' ']
...
So basically I'm just trying to sort out the 'out' file into columns with every eight value in the same column.
Reply


Messages In This Thread
RE: Reading every nth line into a column from txt file - by Laplace12 - Jun-28-2021, 06:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 1,024 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Reading and storing a line of output from pexpect child eagerissac 1 4,302 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Help copying a column from a csv to another file with some extras g0nz0uk 3 495 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
Sad problems with reading csv file. MassiJames 3 674 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading in of line not working? garynewport 2 875 Sep-19-2023, 02:22 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 941 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  [SOLVED] [Windows] Fails reading strings with accents Winfried 1 859 Apr-23-2023, 05:27 PM
Last Post: Larz60+
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,189 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,134 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,647 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan

Forum Jump:

User Panel Messages

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