[英]Python read text file with newline and and paragraph separated elements
I am trying to read a text file to a nested list in Python.我正在尝试将文本文件读取到 Python 中的嵌套列表。 That is, I would like to have the output as:也就是说,我希望输出为:
[[$5.79, Breyers Ice Cream, Homemade Vanilla, 48 oz], [$6.39, Haagen-dazs, Vanilla Bean Ice Cream, 1 pt], etc...]]
The ultimate goal is to read the information into a pandas DataFrame for some exploratory analysis.最终目标是将信息读入 Pandas DataFrame 以进行一些探索性分析。
$5.79
Breyers Ice Cream
Homemade Vanilla
48 oz
$6.39
Haagen-dazs
Vanilla Bean Ice Cream
1 pt
$6.89
So Delicious
Dairy Free Coconutmilk No Sugar Added Dipped Vanilla Bars
4 x 2.3 oz
$5.79
Popsicle Fruit Pops Mango
12 ct
with open(sample.txt) as f:
creams = f.read()
creams = f.split("\n\n")
However, this returns:但是,这将返回:
['$5.79\nBreyers Ice Cream\nHomemade Vanilla\n48 oz', '$6.39\nHaagen-dazs\nVanilla Bean Ice Cream\n1 pt',
I have also tried utilizing list comprehension methods that look cleaner than the above code, but these attempts handle the newlines, not the paragraphs or returns.我还尝试使用看起来比上述代码更清晰的列表理解方法,但这些尝试处理的是换行符,而不是段落或返回。 For example:例如:
[x for x in open('<file_name>.txt').read().splitlines()]
#Gives
['$5.79', 'Breyers Ice Cream', 'Homemade Vanilla', '48 oz', '', '$6.39', 'Haagen-dazs', 'Vanilla Bean Ice Cream', '1 pt', '', '
I know I would need to nest a list within the list comprehension, but I'm unsure how to perform the split.我知道我需要在列表理解中嵌套一个列表,但我不确定如何执行拆分。
Note: This is my first posted question, sorry for the length or lack of brevity.注意:这是我第一次发布的问题,抱歉篇幅过长或不够简洁。 Seeking help because there are similar questions but not with the outcome I desire.寻求帮助,因为有类似的问题,但不是我想要的结果。
You are nearly there once you have the four-line groups separated.一旦您将四行组分开,您就差不多了。 All that's left is to split the groups again by a single newline.剩下的就是用一个换行符再次拆分组。
with open('creams.txt','r') as f:
creams = f.read()
creams = creams.split("\n\n")
creams = [lines.split('\n') for lines in creams]
print(creams)
You just have to split it again.你只需要再次拆分它。
with open('sample.txt','r') as file:
creams = file.read()
creams = creams.split("\n\n")
creams = [lines.split('\n') for lines in creams]
print(creams)
#[['$5.79 ', 'Breyers Ice Cream ', 'Homemade Vanilla ', '48 oz'], ['$6.39 ', 'Haagen-dazs ', 'Vanilla Bean Ice Cream ', '1 pt'], ['$6.89 ', 'So Delicious ', 'Dairy Free Coconutmilk No Sugar Added Dipped Vanilla Bars ', '4 x 2.3 oz'], ['$5.79 ', 'Popsicle Fruit Pops Mango', '-', '12 ct']]
#Convert to Data
df = pd.DataFrame(creams, columns =['Amnt', 'Brand', 'Flavor', 'Qty'])
Amnt Brand \
0 $5.79 Breyers Ice Cream
1 $6.39 Haagen-dazs
2 $6.89 So Delicious
3 $5.79 Popsicle Fruit Pops Mango
Flavor Qty
0 Homemade Vanilla 48 oz
1 Vanilla Bean Ice Cream 1 pt
2 Dairy Free Coconutmilk No Sugar Added Dipped V... 4 x 2.3 oz
3 - 12 ct
Note: I have added -
in the last row for the flavor column as it was empty.注意:我在风味列的最后一行添加了-
因为它是空的。 If your original dataset, you must take this into consideration before performing any analysis.如果是原始数据集,则必须在执行任何分析之前考虑到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.