Python Forum
Write selected (random) columns to output-file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write selected (random) columns to output-file
#1
Hello everyone,

I am about to take a random sample (n = 2) from a tab-separated "ran.txt"-file displayed right below, but I only want to print the first and second column to a new output-file "out.txt".
So far, I have managed to take the sample with the code you can see below. I have used linesstr = ''.join(rl) to join lines to a printable string, which surely is part of my problem.

So my question is: what would be the right code to print line[0] and line[1] to a new file? I was thinking about a loop, but a I don't know how to combine it with the random.sample(input.readlines(). Any ideas?

input-file "ran.txt":
Output:
45378 BNA 125 CALEC 74231 BNA 125 CALEC 469 FSE 176 CALEC 2347893 FSE 176 CALEX 4273897 KLW 089 CALEX
output in "out.txt" should be for example:
Output:
74231 BNA 4273897 KLW
Here is the code and output I achieved the n=2 sample with:
import os
import io
import random

os.chdir("...")
input = io.open("ran.txt","r", encoding="utf-8")
rl = random.sample(input.readlines(),2)

out = open("out.txt", "w", encoding="utf-8")
linesstr = ''.join(rl)
print(linesstr, file=out)

input.close()
output.close()
Output:
74231 BNA 125 CALEC 469 FSE 176 CALEC
I tried to replace the code for writing into the output-file by the following, but it will only give me one random line, not the first column. Obviously, readlines() has to be done in another way...

out = open("out.txt", "w", encoding = "utf-8")
print(rl[0], file=out)
Thanks for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 394 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Last record in file doesn't write to newline gonksoup 3 404 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Create Choices from .ods file columns cspower 3 583 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,501 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,427 Nov-09-2023, 10:56 AM
Last Post: mg24
  Create csv file with 4 columns for process mining thomaskissas33 3 745 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 506 Oct-24-2023, 05:56 AM
Last Post: buran
  How do I read and write a binary file in Python? blackears 6 6,499 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,089 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,586 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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