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
#7
If look at data so should it be turn around this is called transpose() if want data into Pandas for calculation, plot..ect.
If just want display data then can Yoriz method work.

To give example,just using first record.
record = {}
with open('ca_data.txt') as f:
    header = next(f)
    for line in f:
        line = line.strip()
        line = line.replace('Time-zero    ', '')
        line = line.split(':')
        line_1 = line[0].strip()
        line_2 = ''.join(line[1:])
        record[line_1] = line_2.split()

# Read like this so it fill in empty values with None
df = pd.DataFrame.from_dict(record, orient='index')
print(df) 
Output:
Lifetimes (ns) 0.4000 0.1250 2.0446 Std deviations 0.0588 None None Intensities (%) 69.2721 9.6726 21.0553 Channel number 41.5603 None None
No can use transpose(),then it will a useful DataFrame.
>>> df = df.transpose()
>>> df
  Lifetimes (ns) Std deviations Intensities (%) Channel number
0         0.4000         0.0588         69.2721        41.5603
1         0.1250           None          9.6726           None
2         2.0446           None         21.0553           None
Laplace12 likes this post
Reply


Messages In This Thread
RE: Reading every nth line into a column from txt file - by snippsat - Jun-29-2021, 12:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading an ASCII text file and parsing data... oradba4u 2 208 Jun-08-2024, 12:41 AM
Last Post: oradba4u
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 1,521 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Reading and storing a line of output from pexpect child eagerissac 1 4,505 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Help copying a column from a csv to another file with some extras g0nz0uk 3 589 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
Sad problems with reading csv file. MassiJames 3 818 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading in of line not working? garynewport 2 1,016 Sep-19-2023, 02:22 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 1,092 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  [SOLVED] [Windows] Fails reading strings with accents Winfried 1 913 Apr-23-2023, 05:27 PM
Last Post: Larz60+
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,409 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,239 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