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
#1
Hey!

I have a text file that I want to sort out. I've coded this and tried dataframe, but that only prints the last line. The code I have now is this, producing the txt file:

with open(output) as file, open(out, 'w') as file_out:
    for line in file:
        if '2101' in line and found:
            a = line.split()
            print(a[1], file=file_out)
        elif 'Lifetimes' in line and found:
            b = line.split()
            print(b[3], b[4], b[5], file=file_out)
        elif 'Std deviations' in line and found:
#            print(c[3:6])
            c = line
            print(deviations(c), file=file_out)
        elif 'Intensities' in line and found:
            d = line.split()
            print(d[3], d[4], d[5], file=file_out)
        elif 'Time-zero' in line and found:
            e = line.split()
            print(e[4], file=file_out)
        else:
            found = True

#This is what I tried so far
with open(out) as a:
    cpt = 0
    for line in a:
        cpt += 1
        if cpt == 8:
            print(line)
            cpt = 0
The 'out' file is like this:

Number
Value1
Deviation
Value2
Deviation
Value3
Deviation
Number
Value1
Deviation
...
So basically the file is now a list I want to sort so that Lifetimes are all in one column, Value1 in the next, then Deviation, Value2, its deviation so on; I want every 8th value in the same column, and I'm guessing this could somehow be done by creating a loop that prints, skips 7 values and prints the next so that the start number could be changed from 1-7. I need to save the results in another file, so perhaps it'd be easier to code the columns in the 'out' file already without creating so many files, but for now it's enough to get the data sorted properly, so even the simplest code to produce columns from the txt file works!
likes this post
Reply


Messages In This Thread
[Solved] Reading every nth line into a column from txt file - by Laplace12 - Jun-28-2021, 09:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 898 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Reading and storing a line of output from pexpect child eagerissac 1 4,272 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Help copying a column from a csv to another file with some extras g0nz0uk 3 473 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
Sad problems with reading csv file. MassiJames 3 648 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading in of line not working? garynewport 2 854 Sep-19-2023, 02:22 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 923 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  [SOLVED] [Windows] Fails reading strings with accents Winfried 1 838 Apr-23-2023, 05:27 PM
Last Post: Larz60+
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,148 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,113 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,586 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