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'));
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.