如何将其转换为数据框并将其另存为 csv?

卡兰

1

这些数据之前是作为.txt文件提供的我将其转换为.csv格式并尝试将其排序为想要的形式,但失败了。我正在尝试找到转换此数据结构的方法(如下所示):

bakeryA
77300 Baker Street
bun: [10,20,30,10]
donut: [20,10,40,0]
bread: [0,10,15,10]
bakery B
78100 Cerabut St
data not available
bakery C
80300 Sulkeh St
bun: [29,50,20,30]
donut: [10,10,30,10]
bread: [10,15,10,20]

进入这个数据框:

姓名 地址 类型 面粉
面包店A 贝克街 77300 号 好的 10 20 30 10
面包店A 贝克街 77300 号 甜甜圈 20 10 40 0
面包店A 贝克街 77300 号 面包 0 10 15 10
面包店 B 78100 Cerabut 街
面包店 C 80300 苏尔凯街 好的 29 50 20 30
面包店 C 80300 苏尔凯街 甜甜圈 10 10 30 10
面包店 C 80300 苏尔凯街 面包 10 15 10 20

谢谢!

代码不同

这与 Pandas 关系不大,更多与将非结构化源解析为结构化数据有关。尝试这个:

from ast import literal_eval
from enum import IntEnum

class LineType(IntEnum):
    BakeryName = 1
    Address = 2
    Ingredients = 3

data = []
with open('data.txt') as fp:
    line_type = LineType.BakeryName
    for line in fp:
        line = line.strip()

        if line_type == LineType.BakeryName:
            name = line # the current line contains the Bakery Name
            line_type = LineType.Address # the next line is the Bakery Address
        elif line_type == LineType.Address:
            address = line # the current line contains the Bakery Address
            line_type = LineType.Ingredients # the next line contains the Ingredients
        elif line_type == LineType.Ingredients and line == 'data not available':
            data.append({
                'Name': name,
                'Address': address
            }) # no Ingredients info available
            line_type = LineType.BakeryName # next line is Bakery Name
        elif line_type == LineType.Ingredients:
            # if the line does not follow the ingredient's format, we
            # overstepped into the Bakery Name line. Then the next line
            # is Bakery Address
            try:
                bakery_type, ingredients = line.split(':')
                ingredients = literal_eval(ingredients.strip())
                data.append({
                    'Name': name,
                    'Address': address,
                    'type': bakery_type,
                    'salt': ingredients[0],
                    'sugar': ingredients[1],
                    'water': ingredients[2],
                    'flour': ingredients[3],
                })
            except:
                name = line
                line_type = LineType.Address

df = pd.DataFrame(data)

这假设您的数据文件采用所示格式。轻微的偏差(例如,空白线)就会将其抛之脑后。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我如何从TradingView图表中获取数据并将其另存为CSV文件?

使用熊猫将数日长的数据框拆分为半小时的数据框,并将其另存为csv文件

在for循环中设置表并将其另存为csv

Shell脚本从文件列表中提取数据并将其另存为csv

如何使用Excel VBA打开.csv并将其另存为.xlsx

如何使用OpenOffice打开Excel文件并将其另存为CSV

如何从csv文件优雅地创建pyspark数据框并将其转换为Pandas数据框?

如何跳过行,直到在txt文件中找到“关键字”并将其余内容另存为csv?蟒蛇

如何转换JSON文件并将其转换为CSV并使用数据框保持标题

使用BeautifulSoup从html解析表并将其另存为csv时出现问题

Python:遍历 .csv 的 url 并将其另存为另一列

如何将我的输出转换为 .JSON 并将其另存为 .json 文件

如何将4维数组转换为2维并在python中另存为csv

Python将字典转换为数据框并将其导出到CSV文件

如何为另存为CSV的数据框非数字列中的每个元素添加引号“”

将边界框区域转换为遮罩并将其另存为PNG文件

如何将数据帧另存为单独的csv文件?

如何从JSON提取特定部分并将其转换为CSV

如何加载.mat文件并将其转换为.csv文件?

如何对熊猫列进行数学运算并将其另存为新数据框

从数组将str转换为字节并将其保存为CSV(在Python中)

使用 Panda 数据框创建多个 .txt 文件并将其保存为 .csv 文件

下载“类似于csv”的文本数据文件,并将其转换为python中的CSV

如何将预测值转换为二进制变量并将其保存到CSV

Python:读取csv文件并将列另存为变量

如何转置一组列并将输出另存为CSV

将带有列表列的数据框另存为csv文件

拆分数据框,重新排列并另存为单独的 csv 文件

将准备好的布局转换为位图并将其另存为文件中的图像