从字符串中删除第一行和最后一行的空行。

huangapple go评论94阅读模式
英文:

Removing first and last empty lines from a string

问题

我已经翻译好了你的代码,以下是翻译的结果:

str := `
也许我们都应该听听唱片,辞去我们的工作

— gach White —

AZ QUOTES
`

并且想要删除所有的空行。我已经能够删除段落中的空行,代码如下:

str = strings.Replace(str, "\n\n", "\n", -1)
fmt.Println(str)

结果如下:

 
也许我们都应该听听唱片,辞去我们的工作
— gach White —
AZ QUOTES

所以,开头还有几行空行,结尾也有几行空行,如何去掉它们呢?

在我的应用程序中,我正在尝试从同一目录中的所有“png”文件中提取文本,并以漂亮的格式获取它,到目前为止,我的完整代码如下:

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	_ "image/png"
)

func main() {
	var files []string

	root := "."
	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if filepath.Ext(path) == ".png" {
			path = strings.TrimSuffix(path, filepath.Ext(path))
			files = append(files, path)
		}
		return nil
	})
	if err != nil {
		panic(err)
	}
	for _, file := range files {
		fmt.Println(file)

		err = exec.Command("tesseract", file+".png", file).Run()
		if err != nil {
			fmt.Printf("Error: %s\n", err)
		} else {
			b, err := ioutil.ReadFile(file + ".txt") // just pass the file name
			if err != nil {
				fmt.Print(err)
			} else {
				str := string(b) // convert content to a 'string'
				str = strings.Replace(str, "\n\n", "\n", -1)
				fmt.Println(str) // print the content as a 'string'
			}
		}
	}

}
英文:

I've the below text:

str := `
 

Maybe we should all just listen to
records and quit our jobs

— gach White —

AZ QUOTES

 

 

 `

And want to remove ALL empty lines.
I was able to remove the empty lines in the paragraphs as:

str = strings.Replace(str, "\n\n", "\n", -1)
fmt.Println(str)

And ended up with:

 
Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES




So, still have couple of empty lines at the beginning and few empty lines at the end, how can I get red of them?

In my app I'm trying to extract the texts from all "png" files in the same directory, and get it in pretty format, my full code so far is:

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	_ "image/png"
)

func main() {
	var files []string

	root := "."
	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if filepath.Ext(path) == ".png" {
			path = strings.TrimSuffix(path, filepath.Ext(path))
			files = append(files, path)
		}
		return nil
	})
	if err != nil {
		panic(err)
	}
	for _, file := range files {
		fmt.Println(file)

		err = exec.Command(`tesseract`, file+".png", file).Run()
		if err != nil {
			fmt.Printf("Error: %s\n", err)
		} else {
			b, err := ioutil.ReadFile(file + ".txt") // just pass the file name
			if err != nil {
				fmt.Print(err)
			} else {
				str := string(b) // convert content to a 'string'
				str = strings.Replace(str, "\n\n", "\n", -1)
				fmt.Println(str) // print the content as a 'string'
			}
		}
	}

}

答案1

得分: 2

将字符串使用\n分割,并删除分割后的元素中的空格,然后使用\n将它们连接起来。

func trimEmptyNewLines(str string) string{
    strs := strings.Split(str, "\n")
    str = ""
    for _, s := range strs {
        if len(strings.TrimSpace(s)) == 0 {
            continue
        }
        str += s+"\n"
    }
    str = strings.TrimSuffix(str, "\n")

    return str
}

这里运行完整代码

英文:

split the string with \n and remove whitespaces in splitted eliments and then concat them with \n

func trimEmptyNewLines(str string) string{
strs := strings.Split(str, "\n")
str = ""
for _, s := range strs {
if len(strings.TrimSpace(s)) == 0 {
continue
}
str += s+"\n"
}
str = strings.TrimSuffix(str, "\n")
return str
}

run full code here

答案2

得分: 1

你可以使用strings.TrimSpace函数来去除字符串开头和结尾的所有空白字符:

str = strings.TrimSpace(str)
英文:

You can use strings.TrimSpace to remove all leading and trailing whitespace:

str = strings.TrimSpace(str)

答案3

得分: 0

看起来你在中间有空白字符,例如:

\n \n

因此,使用正则表达式 \n[ \t]*\n 进行 regexp replace 可能更合理。

这不会删除开头的单个空行,如果要删除开头的空行,可以使用 ^\n* 并替换为空字符串。


进一步完善,你可以添加更多的空白字符,如 \f,并考虑一次性处理多个空行

\n([ \t\f]*\n)+
  • \n 换行符
  • (...)+ 后跟一个或多个
  • [ \t\f]*\n 空行

这将清除中间的所有空行,但可能会保留字符串开头或结尾的空白字符。如其他答案中建议的,添加 strings.TrimSpace() 可以解决这个问题。


将所有内容放在一起,得到以下代码:

package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
str := `
Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES
`
re := regexp.MustCompile(`\n([ \t\f]*\n)+`)
str = string(re.ReplaceAll([]byte(str), []byte("\n")))
str = strings.TrimSpace(str)
fmt.Println("---")
fmt.Println(str)
fmt.Println("---")
}

最终输出结果为:

---
Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES
---
英文:

It looks like you have white space inbetween, e.g.

\n \n

So, doing a regexp replace with regular expression \n[ \t]*\n might be more sensible.

This won't remove single empty lines at the beginning though, for this you would use ^\n* and replace with an empty string.


Refining this a bit further, you can add more white space like \f and consider multiple empty lines at once

\n([ \t\f]*\n)+
  • \n a newline
  • (...)+ followed by one or more
  • [ \t\f]*\n empty lines

This clears all empty lines in between, but may keep white space at the beginning or the end of the string. As suggested in other answers, adding a strings.TrimSpace() takes care of this.


Putting everything together gives
https://play.golang.org/p/E07ZkE2nlcp

package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
str := `
Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES
`
re := regexp.MustCompile(`\n([ \t\f]*\n)+`)
str = string(re.ReplaceAll([]byte(str), []byte("\n")))
str = strings.TrimSpace(str)
fmt.Println("---")
fmt.Println(str)
fmt.Println("---")
}

which finally shows

---
Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES
---

答案4

得分: 0

我复制了你的字符串并将其转换为JSON:

package main

import (
	"encoding/json"
	"log"
)

func main() {

	// 原始帖子中的字符串。
	myString := `


Maybe we should all just listen to
records and quit our jobs

— gach White —

AZ QUOTES



 `

	// 转换为JSON。
	data, err := json.Marshal(myString)
	if err != nil {
		log.Fatalf("无法将字符串转换为JSON。\n错误:%s", err.Error())
	}

	// 将字符串打印到stdout。
	println(string(data))
}

在JSON中,可能更容易看到空格。

"\n \n\nMaybe we should all just listen to\nrecords and quit our jobs\n\n— gach White —\n\nAZ QUOTES\n\n \n\n \n\n "

你看到问题了吗?在换行符之间有几个空格,此外,换行符的数量不均匀。因此,用\n替换\n\n不会按你的期望行事。

我看到你的一个目标是:

并且想要删除所有空行。

(我不会解决从PNG文件中提取文本的问题,因为那是一个单独的问题。)

package main

import (
	"encoding/json"
	"log"
	"strings"
)

func main() {

	// 原始帖子中的字符串。
	myString := `


Maybe we should all just listen to
records and quit our jobs

— gach White —

AZ QUOTES



 `

	// 创建一个结果字符串。
	result := ""

	// 遍历此字符串中的每一行。
	for _, line := range strings.Split(myString, "\n") {
		if line = strings.TrimSpace(line); line != "" {
			result += line + "\n"
		}
	}

	// 将结果打印到stdout。
	println(result)

	// 将结果转换为JSON。
	resultJSON, err := json.Marshal(result)
	if err != nil {
		log.Fatalf("无法将结果转换为JSON。\n错误:%s", err.Error())
	}

	println(string(resultJSON))
}

stdout:

Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES
"Maybe we should all just listen to\nrecords and quit our jobs\n— gach White —\nAZ QUOTES\n"
英文:

I copied your string and turned it into JSON:

package main

import (
	"encoding/json"
	"log"
)

func main() {

	// The string from the original post.
	myString := `
 

Maybe we should all just listen to
records and quit our jobs

— gach White —

AZ QUOTES

 

 

 `

	// Marshal to json.
	data, err := json.Marshal(myString)
	if err != nil {
		log.Fatalf("Failed to marshal string to JSON.\nError: %s", err.Error())
	}

	// Print the string to stdout.
	println(string(data))
}

It'll probably be easier to see the whitespace in JSON.

"\n \n\nMaybe we should all just listen to\nrecords and quit our jobs\n\n— gach White —\n\nAZ QUOTES\n\n \n\n \n\n "

Do you see the problem here? There's a couple of spaces in between your newline characters, additionally, you have uneven numbers of newline characters. so replacing \n\n with \n won't behave as you'd like it to.

I see one of your goals is this:
> And want to remove ALL empty lines.

(I'm not addressing extracting text from PNG files, as that's a separate question.)

package main

import (
	"encoding/json"
	"log"
	"strings"
)

func main() {

	// The string from the original post.
	myString := `
 

Maybe we should all just listen to
records and quit our jobs

— gach White —

AZ QUOTES

 

 

 `

	// Create a resulting string.
	result := ""

	// Iterate through the lines in this string.
	for _, line := range strings.Split(myString, "\n") {
		if line = strings.TrimSpace(line); line != "" {
			result += line + "\n"
		}
	}

	// Print the result to stdout.
	println(result)

	// Marshal the result to JSON.
	resultJSON, err := json.Marshal(result)
	if err != nil {
		log.Fatalf("Failed to marshal result to JSON.\nError: %s", err.Error())
	}

	println(string(resultJSON))
}

stdout:

Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES
"Maybe we should all just listen to\nrecords and quit our jobs\n— gach White —\nAZ QUOTES\n"

答案5

得分: 0

一个稍微不同的答案。

package main
import (
"fmt"
)
func main() {
str := `
或许我们都应该听听唱片,辞去工作
— gach White —
AZ QUOTES
`    
first := 0
last := 0
for i, j := range []byte(str) {
if j != 10 && j != 32 {
if first == 0 {
first = i
}
last = i
}
}
str = str[first : last+1]
fmt.Print(str)
}
英文:

A little different answer.

package main
import (
"fmt"
)
func main() {
str := `
Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES
`    
first := 0
last := 0
for i, j := range []byte(str) {
if j != 10 && j != 32 {
if first == 0 {
first = i
}
last = i
}
}
str = str[first : last+1]
fmt.Print(str)
}

huangapple
  • 本文由 发表于 2021年7月30日 01:59:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/68580866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定