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
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,043 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 931 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 907 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,541 Oct-17-2024, 01:15 AM
Last Post: Winfried
  What does .flush do? How can I change this to write to the file? Pedroski55 3 1,341 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 1,471 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Last record in file doesn't write to newline gonksoup 3 1,576 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Create Choices from .ods file columns cspower 3 1,757 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 5,095 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,848 Nov-09-2023, 10:56 AM
Last Post: mg24

Forum Jump:

User Panel Messages

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