Python:如何读取文件中的行,其间有两个行间距

时间:2016-04-16 19:25:26

标签: python pandas dataframe readfile

我正在尝试读取一个格式如下的文件:每行之间有两个'\ n'空格。

open_reviews = open("C:\\Downloads\\review_short.txt","r",encoding="Latin-1" ).read()
documents = []
for r in open_reviews.split('\n\n'):
    documents.append(r)

df = pd.DataFrame(documents)
print(df.head())

我使用下面的python代码来读取该行并将其转换为Dataframe:

0  I was very inspired by Louise's Hay approach t...
1  \n You Can Heal Your Life by 
2  \n I had an older version
3  \n I love Louise Hay and
4  \n I thought the book was exellent

我得到的输出如下:

0  I was very inspired by Louise's Hay approach t...
1  You Can Heal Your Life by 
2  I had an older version
3  I love Louise Hay and
4  I thought the book was exellent

由于我使用了两个(\ n),它会在每行的开头附加。有没有其他方法来处理这个,所以我得到如下输出:

{{1}}

3 个答案:

答案 0 :(得分:2)

这会附加每个非空行。

filename = "..."
lines = []
with open(filename) as f:
    for line in f:
        line = line.strip()
        if line:
            lines.append(line)

>>> lines
['Great tool for healing your life--if you are ready to change your beliefs!<br /><a href="http',
 'Bought this book for a friend. I read it years ago and it is one of those books you keep forever. Love it!',
 'I read this book many years ago and have heard Louise Hay speak a couple of times.  It is a valuable read...']

lines = pd.DataFrame(lines, columns=['my_text'])
>>> lines
                                             my_text
0  Great tool for healing your life--if you are r...
1  Bought this book for a friend. I read it years...
2  I read this book many years ago and have heard...

答案 1 :(得分:0)

尝试使用.stip()方法。它将从字符串的开头或结尾删除任何不必要的空白字符。

你可以像这样使用它:

for r in open_review.split('\n\n'):
    documents.append(r.strip())

答案 2 :(得分:0)

使用readlines()并使用strip()清除该行。

filename = "C:\\Downloads\\review_short.txt"
open_reviews = open(filename, "r", encoding="Latin-1")
documents = []
for r in open_reviews.readlines():
    r = r.strip()  # clean spaces and \n
    if r:
        documents.append(r)