Python - 将数据写入数据框正在从下一个可用位置重写空集

Python - Writing data to a data frame is rewriting the empty sets from the next available location

我有一个包含近 600 个属性的大型文本文件。我正在尝试将这些转换为 csv 文件。因此,我加载了文本文件并创建了一个数据框并将它们加载到一个 csv 文件中。下面是我的数据列表,它有 2 个属性(我的 属性 集中确实有缺失值 - 这里第二组中缺少每季度的价格),

Name of the Property : North Kensington Upcycling Store and Cafe
Availability : Now 
Area :  1,200 sqft
Retail Type  : No
Bar & Restaurant Type  : No
Event Type  : Yes
Shop Share Type  : No
Unique Type  : No
Price Per Day : £360
Price Per Week : £1,260
Price Per Month : £5,460
Price Per Quarter : £16,380
Price Per Year : £65,520
[Latitude, Longitude] : [51.5235108631773, -0.206594467163086]
Name of the Property : Old Charlton Pub
Availability : Now 
Area :  1,250 sqft
Retail Type  : No
Bar & Restaurant Type  : Yes
Event Type  : No
Shop Share Type  : No
Unique Type  : No
Price Per Day : £70
Price Per Week : £490
Price Per Month : £2,129
Price Per Year : £25,550
[Latitude, Longitude] : [51.4926332979245, 0.0449645519256592]

这是我写的代码 -

import pandas
import csv

txt_file = r"sa4.txt"
txt = open(txt_file, "r")
txt_string = txt.read()
txt_lines = txt_string.split("\n")
txt_dict = {}

for txt_line in txt_lines:
    k,v = txt_line.split(":")
    k = k.strip()
    v = v.strip()
    if k in txt_dict:
        list = txt_dict.get(k)
    else:
        list = []
    list.append(v)
    txt_dict[k]=list
print(df)
df.to_csv("MYFILE2.csv")

这是我的输出 csv 文件 - Picture 我不知道 为什么第二个 属性 的每季度价格值来自第五个 属性 每季度价格 [ 下一个可用位置是哪个 ]? IT 必须为 NULL,但它已变成 £135000 。任何人都可以在我的代码中看到问题吗?提前致谢。

这仅仅是因为您的列表在丢失时没有每季度价格的占位符。要解决它,您必须跟踪 属性 记录号。像这样的东西应该适合你:

import pandas as pd

txt_file = r"sa4.txt"
txt = open(txt_file, "r")
txt_string = txt.read()
txt_lines = txt_string.split("\n")
df = pd.DataFrame()
idx = -1 # This will make sense in the `if` block below

for txt_line in txt_lines:
    k,v = txt_line.split(":")
    k = k.strip()
    v = v.strip()
    if k == 'Name of the Property':
        idx += 1 # Now, idx will be 0 for the first run
    df.loc[idx, k] = v
print(df)
df.to_csv("MYFILE2.csv")