Python——用逻辑创造!如何将这样的文本文件转换为CSV文件?

Python——用逻辑创造!如何将这样的文本文件转换为CSV文件?,python,export-to-excel,export-to-csv,Python,Export To Excel,Export To Csv,我有一个文本文件,它的数据格式为column:row。我想将此文本文件加载到python中,并将其转换为csv或excel文件,然后保存在本地桌面上。有人有逻辑吗?请分享你的答案。提前谢谢。下面是我在文本文件中的数据示例 Name of the Property : North Kensington Upcycling Store and Cafe Availability : Now Interest Level : 74 people are looking right now Area

我有一个文本文件,它的数据格式为column:row。我想将此文本文件加载到python中,并将其转换为csv或excel文件,然后保存在本地桌面上。有人有逻辑吗?请分享你的答案。提前谢谢。下面是我在文本文件中的数据示例

Name of the Property : North Kensington Upcycling Store and Cafe
Availability : Now 
Interest Level : 74 people are looking right 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 
Interest Level : 20 people are looking right 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
txt_file = r"patty.txt"
txt = open(txt_file, "r")
txt_string = txt.read()
txt_lines = txt_string.split("\n")
txt_dict = {}

for txt_line in txt_lines:
    print(txt_line)
    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 (pandas.DataFrame.from_dict(txt_dict, orient="index"))

获取此错误:-k,v=txt\u line.split(“:”)value错误:需要超过1个值才能解包

您将使用熊猫,并将其导入。它允许您读取.txt数据(不要混淆,尽管函数名为“read.csv()”,但这是可能的


请随意阅读,以获取诸如“sep”或“encoding”之类的关键字的详细信息(此处需要这些关键字,因为这里有英镑符号)"

读取文件,解析文件,将数据写入CSV。@Mooningrawr但我确实有一些缺少的值,比如在本例中-我没有第二组的季度价格数据。@Mooningrawr请您为其编写一个示例代码。我是一名初学者,刚刚开始学习。如果您尝试,学习如何编写代码是最好的,这就像游泳一样,当然我们可以向您展示你会游泳,你可以模仿我们,但如果你不尝试,你永远都不会成为一名优秀的游泳运动员。那么,你如何阅读如何做事情,尝试一下,然后当你失败时回到我们身边;我们会很乐意帮你解决这个问题。不管怎么说,在这里缺乏努力通常是不受欢迎的。是的,我理解。我正在努力解决问题。不管怎样,Thanx谢谢。但输出不是结构化的。我要找的是一个正常的结构化的。值在:的左侧作为列名,右侧作为行,如果缺少值,如每季度价格,我希望它在那里有一个NA。如此复杂的一个!:(一步一步走。我只是想指出一个方向,而不是解决你的工作/家庭作业很酷,很酷!!谢谢。让我来做这件事,如果我真的被击中了,我会回来的。这就是精神!我这样做的想法是,将此数据作为文本文件加载,然后加载另一个CSV文件,其中只包含所需的列名和概率。)把它们放在一起,行吗?
import pandas as pd
df = pd.read_csv('text.txt', sep = ':', encoding = "ISO-8859-1")

df.to_csv('text.csv', encoding = "ISO-8859-1", sep=';')