Converting comma separated .txt file into dictionary - python

I have a comma delimited .txt file and I need to convert the list into key:value pairs in python 3:
Below is the .txt file:
1988,0.4891
1989,0.2830
1990,0.4312
1991,0.1251
1992,0.0181
1993,0.6182
1994,0.1587
1995,0.1409
1996,0.1505
1997,0.0994
1998,0.1631
1999,0.0330
2000,0.0523
2001,-0.0798
2002,0.1107
2003,0.2308
2004,0.0484
2005,0.0114
2006,0.1088
2007,0.0228
2008,0.1538
2009,0.0038
2010,0.1085
2011,-0.0631
2012,-0.1581
2013,0.2538
2014,0.1377
2015,0.0199
2016,-0.0392
2017,0.0433
2018,-0.0154
Here is my python code:
import csv
answer = {}
with open("annual_chesapeakeCapital_diversifiedProgramLV.txt") as infile:
keys = infile.readline().split(",")
values = infile.readline().split("\n")
answer = dict(zip(keys, values))
print(answer)
What am I doing wrong?

You can use csv.reader to read the rows into a sequence of two-item lists, so that you can pass it to the dict constructor to build the dict you're looking for:
with open("annual_chesapeakeCapital_diversifiedProgramLV.txt") as infile:
answer = dict(csv.reader(infile))

To specifically answer your question, what you are doing wrong is that readline() only reads one line - you need to iterate reading all the lines and splitting each of them, and using the result of each split to create entries in the dictionary.
BTW dictionaries can by definition only have one entry for each key, so have you planned what will happen if two lines have the same value before the comma?

With pandas it's easy
import pandas as pd
df=pd.read_csv("annual_chesapeakeCapital_diversifiedProgramLV.txt",header=None)
answer =dict(zip(df[0].values,df[1].values))

Related

Writing strings to CSV causing issue where the string in CSV is separated by commas (Python)

I am facing an issue that I was not able to resolve so far. I need to save a list of strings into the CSV file. I am able to do it however the strings from each element of the list are separated by commas. Why is that and what do I need to do to resolve this issue? Sorry for maybe simple question I am new to programming. I know it has to be somehow related to the string properties where each number is similar to an item in the list and is indexed however I was not able to find the cause of this behavior and how to resolve it.
Here is the code:
duplicity = ['8110278643', '8110278830', '8110283186']
with open("duplicty.csv", "w", newline="") as duplicity_csv:
output_writer = csv.writer(duplicity_csv)
header = ["Number"]
output_writer.writerow(header)
for item in duplicity:
output_writer.writerow(item)
The output of this code in CSV is following:
Number
8,1,1,0,2,7,8,6,4,3
8,1,1,0,2,7,8,8,3,0
8,1,1,0,2,8,3,1,8,6
The expected output should be:
Number
8110278643
8110278830
8110283186
Thanks a lot for your replies!
The writerow method takes an iterable of strings. Each item in your list is in fact an iterable -- namely a string. Each element from that iterable (in your case each letter in the string) is taken as its own element in a sperate column.
You could just do this instead:
...
for item in duplicity:
output_writer.writerow([item])
Use writerows, for example:
duplicity = ['8110278643', '8110278830', '8110283186']
with open("duplicty.csv", "w", newline="") as duplicity_csv:
output_writer = csv.writer(duplicity_csv)
header = ["Number"]
output_writer.writerows([row] for row in header + duplicity)
writerow() needs list with items (even when you have single item in row) but you use single string and it treads it as list of chars
You need
output_writer.writerow( [item] )

How to parse dictionary syntaxed string to dictionary object

I have a file syntaxed in a way that ressembles a Dictionary as follows:
{YEARS:5}
{GROUPS:[1,2]}
{SAVE_FILE:{USE:1,NAME:CustomCalendar.ics}}
{SAVE_ONLINE:{USE:1,NAME:Custom Calendar,EMAIL:an.email#something.com,PASSWORD:AcompLExP#ssw0rd}}
{COURSES:[BTC,CIT,CQN,OSA,PRJCQN,PRJIOT,PILS,SPO,SHS1]}
I would like to find a way to parse each individual line into a dictionary as it is written. The difficulty I have is that some of these lines contain a dictionary as their value.
I am capable of taking the single lines and converting them to actual dictionaries but I am having an issue when working with the other lines.
Here is the code I have so far:
def get_config(filename=):
with open(filename, encoding="utf8") as config:
years = config.read().split()[0]
print(parse_line(years))
def parse_line(input_line):
input_line = input_line.strip("{}")
input_line = input_line.split(":")
return {input_line[i]: input_line[i + 1] for i in range(0, len(input_line), 2)}
If at all possible, I'd love to be able to deal with any line within a single function and hopefully deal with more than two nested dictionaries.
Thanks in advance!
If your file would contain valid JSON format, it would be an easy task to read the file and convert your data structures to dictionaries.
To give an example, consider having the following line of text in a file text.txt:
{"SAVE_ONLINE":{"USE":1,"NAME":"Custom Calendar","EMAIL":"an.email#something.com","PASSWORD":"AcompLExP#ssw0rd"}}
Please note, that the only difference are the quotes " around strings.
You can easily parse the line to a dictionary structure with:
import json
with open('text.txt', 'r') as f:
d = json.loads(f.read())
Output
print(d)
# {'SAVE_ONLINE': {'USE': 1, 'NAME': 'Custom Calendar', 'EMAIL': 'an.email#something.com', 'PASSWORD': 'AcompLExP#ssw0rd'}}

Splitting a string based on a list

I have a list like this:
destinations = ['one', 'two', 'three']. I have an input string that I am splitting like this: one, two, three = re.split(r'\t', line). What I'd like to do is have the split operation fill a dictionary, with the destinations entries being the keys of the dictionary, and the fields from the line being the values. Is this possible with python?
Use the zip() function to pair up destinations and the output of re.split() into key-value pairs, then pass that to dict():
dictionary = dict(zip(destinations, re.split(r'\t', line)))
I suspect you are trying to read tab-separated CSV data; try not to reinvent the wheel and use the csv.DictReader() class instead. It'll even read the fieldnames from the first row of the file, if so required.

Writing multiple values in single cell in csv

For each user I have the list of events in which he participated.
e.g. bob : [event1,event2,...]
I want to write it in csv file. I created a dictionary (key - user & value - list of events)
I wrote it in csv. The following is the sample output
username, frnds
"abc" ['event1','event2']
where username is first col and frnds 2nd col
This is code
writer = csv.writer(open('eventlist.csv', 'ab'))
for key, value in evnt_list.items():
writer.writerow([key, value])
when I am reading the csv I am not getting the list directly. But I am getting it in following way
['e','v','e','n','t','1','','...]
I also tried to write the list directly in csv but while reading am getting the same output.
What I want is multiple values in a single cell so that when I read a column for a row I get list of all events.
e.g
colA colB
user1,event1,event2,...
I think it's not difficult but somehow I am not getting it.
###Reading
I am reading it with the help of following
codereader = csv.reader(open("eventlist.csv"))
reader.next()
for row in reader:
tmp=row[1]
print tmp # it is printing the whole list but
print tmp[0] #the output is [
print tmp[1] #output is 'e' it should have been 'event1'
print tmp[2] #output is 'v' it should have been 'event2'
you have to format your values into a single string:
with open('eventlist.csv', 'ab') as f:
writer = csv.writer(f, delimiter=' ')
for key, value in evnt_list.items():
writer.writerow([key, ','.join(value)])
exports as
key1 val11,val12,val13
key2 val21,val22,val23
READING: Here you have to keep in mind, that you converted your Python list into a formatted string. Therefore you cannot use standard csv tools to read it:
with open("eventlist.csv") as f:
csvr = csv.reader(f, delimiter=' ')
csvr.next()
for rec in csvr:
key, values_txt = rec
values = values_txt.split(',')
print key, values
works as awaited.
You seem to be saying that your evnt_list is a dictionary whose keys are strings and whose values are lists of strings. If so, then the CSV-writing code you've given in your question will write a string representation of a Python list into the second column. When you read anything in from CSV, it will just be a string, so once again you'll have a string representation of your list. For example, if you have a cell that contains "['event1', 'event2']" you will be reading in a string whose first character (at position 0) is [, second character is ', third character is e, etc. (I don't think your tmp[1] is right; I think it is really ', not e.)
It sounds like you want to reconstruct the Python object, in this case a list of strings. To do that, use ast.literal_eval:
import ast
cell_string_value = "['event1', 'event2']"
cell_object = ast.literal_eval(cell_string_value)
Incidentally, the reason to use ast.literal_eval instead of just eval is safety. eval allows arbitrary Python expressions and is thus a security risk.
Also, what is the purpose of the CSV, if you want to get the list back as a list? Will people be reading it (in Excel or something)? If not, then you may want to simply save the evnt_list object using pickle or json, and not bother with the CSV at all.
Edit: I should have read more carefully; the data from evnt_list is being appended to the CSV, and neither pickle nor json is easily appendable. So I suppose CSV is a reasonable and lightweight way to accumulate the data. A full-blown database might be better, but that would not be as lightweight.

converting a list of strings into a tuple

Hello i am trying to do a dictionary by using Python,
What needs to be done is python reads a text file which has values inside such as:
good buono
What I have done was, open the file with file function and replace tabs and add reversed comma to create a list so it looks like
["('good', 'buono')", "('afternoon', 'pomeriggo')",... and so on
but the problem is type of each word translation is not a tuple, it is string when I am trying to see 1st element(number 0) it shows me the value as
"('good', 'buono')"
which is a string. I need to use dict formula so that i can convert the type into dictionary but I cannot because it is list of strings(has to be list of tuples)
So how can I convert that list of strings into list of tuples?
Assuming that every pair of words is on a separate line in your file, you could do this:
mydict = {line.split()[0]: line.split()[1] for line in myfile}
This transforms a file like
good buono
afternoon pomeriggo
thanks grazie
into
{"good": "buono", "afternoon": "pomeriggo", "thanks": "grazie"}
No need for tuples along the way.
.split() splits the input string on whitespace, removing any leading/trailing whitespace:
>>> " good\tbuono \n".split()
['good', 'buono']
with open('input.txt') as f:
result = map(str.split, f)
# -> [['good', 'buono'], ['afternoon', 'pomeriggo']]
d = dict(result)
# -> {'good': 'buono', 'afternoon': 'pomeriggo'}
ast will work as #onemach suggested, but you may want to read the strings in as tuples instead.
Snippet:
li = []
with open("file.txt", 'r') as f:
li.append(tuple(f.readline().split('\t')))
This will split the string about the tab, and place it into a tuple object, which is then appended into a list.

Categories