-3

I have a text file which has the data in the form of column : row. I would like to load this text file into python and convert it into csv or an excel file and save it in local desktop. Anyone have a logic? Kindly share your answers. Thanks in advance. Here goes a sample of my data in the text file,

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]

This is the code that I am trying,

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"))

and getting this error :- k,v = txt_line.split(" : ") ValueError: need more than 1 value to unpack

roy
  • 3
  • 1
  • 5
  • 2
    Read File, Parse File, Write Data To CSV. – MooingRawr Nov 11 '16 at 15:50
  • @MooingRawr but I do have some missing values, say in this case - I don't have the Price Per Quarter data in the second set. – roy Nov 11 '16 at 16:13
  • @MooingRawr Please can you write a sample code for it. Am a beginner and just getting along. – roy Nov 11 '16 at 16:15
  • Learning how to code is best if you try it out, it's like swimming, sure we can show you how to swim and you can copy us, but if you never try you will never be a good swimmer. So how about you read up on how to do things, give it a try, and then come back to us when you failed; we will gladly help you out that way. Lack of effort is generally frowned upon here anyways. – MooingRawr Nov 11 '16 at 16:17
  • Ya I understand. I am trying to get on with things. Thanx anyways – roy Nov 11 '16 at 16:19
  • Your line doesn't contain a colon, for that error... By the way, http://stackoverflow.com/questions/8009882/how-to-read-large-file-line-by-line-in-python – OneCricketeer Nov 15 '16 at 13:15

1 Answers1

0

You will use pandas, which you will import. It allows you to read .txt data (dont be confused, this is possible although the function is called "read.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=';')

Feel free to read the documentation for getting details on the keywords such as "sep" or "encoding" (which is required here since there is a pound symbol"

Nikolas Rieble
  • 1,761
  • 14
  • 38
  • Thanks for that. But the output isn't a structured one. What I am looking for is a normal structured one. With the values on the left side of : as Column name and right side of : as a Row and when there are missing values like Price Per Quarter, I want it to have a NA over there. Such a complex one ! :( – roy Nov 11 '16 at 16:51
  • Step by step is the way to go. I am merely trying to point a direction rather than solving your work/homework – Nikolas Rieble Nov 11 '16 at 16:51
  • 1
    Cool, Cool !! Thanks. Let me work on this and I will get back if I do get struck somewhere. – roy Nov 11 '16 at 16:53
  • Thats the spirit! – Nikolas Rieble Nov 11 '16 at 16:53
  • My Idea of doing this was to, load this data as a text file and load another CSV File with only the required column names and probably parse them together. Will it work ? – roy Nov 14 '16 at 13:00
  • unless you provide some code and specific the question regarding your code and the error it yields, the question will be closed. – Nikolas Rieble Nov 14 '16 at 14:56
  • Sorry if i Frustrated you. I have tried the code that I updated in the question. Got an error with the final step. Can you rectify that ? Thanks – roy Nov 15 '16 at 13:02
  • You did not at all frustrate me :) Thank you for your consideration. I am only warning you since other members of this community consider your question to be of poor quality - which is why it has been put "on hold" – Nikolas Rieble Nov 15 '16 at 15:55