Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将文本块解析为行?_Python - Fatal编程技术网

Python 如何将文本块解析为行?

Python 如何将文本块解析为行?,python,Python,我们正在尝试将文本块解析为单独的行。它保存为文本文档,我们的目标是将单独的文本块分配到单独的行上 ggplot2 is a data visualization package for the statistical programming language R. Created by Hadley Wickham in 2005, ggplot2 is an implementation of Leland Wilkinson's Grammar of Graphics—a general s

我们正在尝试将文本块解析为单独的行。它保存为文本文档,我们的目标是将单独的文本块分配到单独的行上

ggplot2 is a data visualization package for the statistical programming language R. Created by Hadley Wickham in 2005, ggplot2 is an implementation of Leland Wilkinson's Grammar of Graphics—a general scheme for data visualization which breaks up graphs into semantic components such as scales and layers. ggplot2 can serve as a replacement for the base graphics in R and contains a number of defaults for web and print display of common scales. Since 2005, ggplot2 has grown in use to become one of the most popular R packages.[1][2] It is licensed under GNU GPL v2.[3]
资料来源:

我想制作一个表格,其中有一个新行,包含“ggplot”后面的文本

行文本分隔符
1 ggplot2是统计编程语言R的数据可视化包,由Hadley Wickham于2005年创建,“ggplot2”
2 ggplot2是Leland Wilkinson图形语法的一个实现,该语法是数据可视化的通用方案,它将图形分解为语义组件,如比例和层。“GG2”
3 ggplot2可替代R中的基本图形,并包含许多常用比例的web和打印显示默认值。自2005年以来,“ggplot2”
4 ggplot2已成为最受欢迎的R软件包之一。[1][2]它是根据GNU GPL v2获得许可的。[3]“ggplot2”
格式设置已关闭,但每行的分隔符列为“ggplot2”

这就是我试过的

text = open('ggplot2.txt','r+')
l=[]
for i in text.readlines():
    if i == "ggplot2":
        l.newline(i)

AttributeError:“列表”对象没有属性“换行” 记住,如果要将项目添加到列表中,则需要附加属性。
例如:

table.append(item)
我想你应该试试

text = open('ggplot2.txt','r+')
table=[]
for row in text.readlines():
    if "ggplot2" in row:
        data = row.split('ggplot2')
        for index, e in enumerate(data):
            table.append([index, 'ggplot2 {0}'.format(e), 'ggplot2'])

print(table)
列表没有名为newline的属性,您可能是指append。

您可以使用
.append()
创建行,并按
“ggplot2”
拆分以获得所需的行:

text = "ggplot2 is a data visualization package for the statistical programming language R. Created by Hadley Wickham in 2005, ggplot2 is an implementation of Leland Wilkinson's Grammar of Graphics—a general scheme for data visualization which breaks up graphs into semantic components such as scales and layers. ggplot2 can serve as a replacement for the base graphics in R and contains a number of defaults for web and print display of common scales. Since 2005, ggplot2 has grown in use to become one of the most popular R packages.[1][2] It is licensed under GNU GPL v2.[3]"

lines = text.split("ggplot2")
rows = []

for line in lines:
  if(line != ""):
    rows.append("ggplot2" + line)

print(rows)

在上面的代码中执行
i==“ggplot2”
的问题是,它检查解析文本的整行是否等于字符串
“ggplot2”
,而不是它是否包含字符串
“ggplot2”

您希望
l.newline(i)
做什么?我可以生成text.split()是否基于多个条件断开新行?(例如,“ggplots2”,“可视化”,“哈德利”)@Sebastian,太好了。是否可以添加一个指示符,该指示符触发了要拆分的行?我正在考虑对delim执行
分隔符:if(delim in line):rows.append(delim)
@Sebastian-Sure。您可以在分割上使用匹配的
r”(ggplot2 | visualization | Hadley)
,以便保留指示器。然后,当您循环时,您可以跟踪它们并将它们添加到您的append:。