[JAVA] 12. Save the first column in col1.txt and the second column in col2.txt

12. Save the first column in col1.txt and the second column in col2.txt

Save only the first column of each row as col1.txt and the second column as col2.txt. Use the cut command for confirmation.

Go

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	//Specify the read file
	name := "../hightemp.txt"
	w1name := "col1.txt"
	w2name := "col2.txt"

	//Open the file to read
	f, err := os.Open(name)
	if err != nil {
		fmt.Printf("os.Open: %#v\n",err)
		return
	}
	defer f.Close() //Crease at the end

	//Create a save file for the first column
	w1, err := os.Create(w1name)
	if err != nil {
		fmt.Printf("os.Open: %#v\n",err)
		return
	}
	defer w1.Close() //Crease at the end

	//Create a second column save file
	w2, err := os.Create(w2name)
	if err != nil {
		fmt.Printf("os.Open: %#v\n",err)
		return
	}
	defer w2.Close() //Crease at the end

	//Create a scanner library
	scanner := bufio.NewScanner(f)

	//Read one line of data
	for scanner.Scan() {
		//Split by TAB
		clm := strings.Split(scanner.Text(),"\t")
		w1.WriteString(clm[0] + "\n")
		w2.WriteString(clm[1] + "\n")
	}

	//Check if there was an error
	if err = scanner.Err(); err != nil {
		fmt.Printf("scanner.Err: %#v\n",err)
		return
	}
}

python

#Open the file to read
with open("../hightemp.txt", "r") as r:
    #Open the first export file
    with open("col1.txt", "w") as w1:
        #Open the second export file
        with open("col2.txt", "w") as w2:
            #Read line by line
            for data in r:
                #Arrange TAB by delimiter
                col = data.strip().split("\t")

                #1 item col1.Export to txt
                w1.writelines(col[0] + "\n")

                #2 items col2.Export to txt
                w2.writelines(col[1] + "\n")

Javascript

//Module loading
var fs = require("fs");
var col1 = [];
var col2 = [];

//Read a text file
var col = fs.readFileSync("../hightemp.txt", 'utf-8');

//Split a string with a line break
var data = col.split('\n');

//Line count loop
data.forEach(function( value ) {
    //Divide items by TAB
    val = value.split('\t')

    //Push data to each array
    col1.push(val[0]);
    col2.push(val[1]);
});

//  col1.txt , col2.Output text concatenated with line breaks to txt
fs.writeFileSync("col1.txt",col1.join('\n'));
fs.writeFileSync("col2.txt",col2.join('\n'));

Summary

The Close processing of Go and Python is neat and good. Deeper Python indentation.

Javascript changed to synchronous processing. Does asynchronous wait somewhere? This is all written in buffer processing.

3 The source is not refreshing when processing files.

Top

Recommended Posts

12. Save the first column in col1.txt and the second column in col2.txt
Select the required variables in TensorFlow and save / restore
Save the pystan model and results in a pickle file
Save the binary file in Python
The first step in Python Matplotlib
About the need for the first slash in the subscriber name and publisher name
[Python3] Save the mean and covariance matrix in json with pandas
I want to make the second line the column name in pandas
MongoDB for the first time in Python
Find it in the procession and edit it
Save the specified channel ID in text and load it at the next startup
The first step to log analysis (how to format and put log data in Pandas)
The first step in the constraint satisfaction problem in Python
About the difference between "==" and "is" in python
When the axis and label overlap in matplotlib
Put the second axis in 2dhistgram of matplotlib
Extract the lyrics information in the MP3 / MP4 file and save it in the lyrics file (* .lrc) for Sony walkman.
13. Merge col1.txt and col2.txt
The simplest Python memo in Japan (classes and objects)
Investigate the relationship between TensorFlow and Keras in transition
Extract and list personal names and place names in the text
Receive the form in Python and do various things
Linux is something like that in the first place
Check if the expected column exists in Pandas DataFrame
Register a task in cron for the first time
Carefully understand the exponential distribution and draw in Python
Read the csv file and display it in the browser
Plot and understand the multivariate normal distribution in Python
[Tips] Save / copy the graph displayed in Jupyter Lab
Carefully understand the Poisson distribution and draw in Python
Automatically save .py and .html files in Jupyter notebook.
Find the Hermitian matrix and its eigenvalues in Python
Automatically access the flow in enebular and pull the trigger
Save and manage Qiita articles in Zenn format Markdown
Runner-up in the first hackathon of the Zukoke rookie trio!
Participated in the first ISUCON with the team "Lunch" # ISUCON10 Qualifying
I touched Wagtail (1) and let's override the save method.
Until the introduction and adoption of the research proposal that won first place in KDD CUP 2019
Shuffle the images in any directory with Python and save them in another folder with serial numbers.
Steps to change table and column names in your Django model at the same time