如何将多行文本变成一行并添加一些逗号

How to make multiple line text into a single line and add some commas

我想将多行 txt 文件(在记事本++上阅读)制作成带有一些逗号的单行文本。文本示例

123
123
123
123

to:

123,123,123,123

可以用notepad++实现吗?如果没有,我该怎么做?

使用find/replace。将您的行终止符放在查找和替换中,然后全部替换。如果您使用 Windows,您的行终止符可能是 \r\n,但也可能是 \n 或其他一些值。您需要将模式设置为扩展或正则表达式(正则表达式)。

你可以在 notepad++ 中完成,我将用 python 编程语言编写我的解决方案。

# You open the file
file = open("Location of the file right click on the text file and copy relative file.")
# You read the whole file and put it in a list where each line would be one 
# item in the list.
readFile = file.read().split("\n")
# Set a new variable so you can concatinate a string
text = ""
# Loop through the list of rows
for i in range(0, len(readFile), 1):
      # Concatinate the each row into one string and add a comma and a space.
     text = text + readFile[i] + ", "
# Print the text
print(text)

这是您可以执行此操作的一种方法。

  • 查找内容:\R
  • 替换为:,

其中 \R 代表任何类型的换行符(即 \r\n\r\n