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.
Related
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] )
I have a data set like this:
and the objective is to have each cell of the data frame in one list whereby each cell is a word in itself.
I have tried originally with:
string = []
for words in data:
string.append(words)
but it gives me the wanted result for the first row only:
.
I have tried also to iterate it using iterrows but it creates just pairs of value not very useful.
How I can iterate the append function over all the rows and store the results as a single string?
I solved it with this:
string = []
for words in data.itertuples(index=False):
string += words
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))
I will try to provide example for the question.
Let's say we have 3 lists. e.g :-
list1 =['one','two','three']
list2=['a','b','c']
list3=['mike','jack','ram']
Or, say there are list values for each lines in the file.
['one','two','three']
['a','b','c']
['mike','jack','ram']
Now I want to write the three lists to three different files by creating them. The names of the files should be autogenerated e.g:-
file001.txt
file002.txt
file003.txt
I am assuming that your data is in the console and each list is a line.
something like this:
line1 =['one','two','three']
line2=['a','b','c']
line3=['mike','jack','ram']
I merged all the data into one lists of list
all_data = [line1] + [line2] + [line3]
This above part is not necessary if all the list values are line by line in one variable. If not you can merge them using some method.
Now, write each line (list values) to the different file:
count = 1
for data in all_data:
output = open('file' + str(count) + '.txt', 'w')
output.write(','.join(data))
count += 1
output.close()
This keeps going on until the last value of the list. So, based on how many lists are there. If you want to join the values inside the list you can change the ''.join with something desirable in the single quotes ('').
Hope I helped.
You can see a detailed explanation here . But to sum it all up , you define an object of the type file , by opening a file(or creating one if it doesn't exist) , and then writing / reading / etc...
Use enumerate and string formatting to construct the file names.
s = 'file{:03}.txt'
for n, lyst in enumerate((list1, list2, list3), 1):
fname = s.format(n)
with open(fname, 'w') as f:
#f.write(','.join(lyst))
f.write('\n'.join(lyst))
If any of the items are not strings change the write to
f.write('\n'.join(map(str, lyst)))
If the lists are so long that creating a single string to write to the file is prohibitive, change the write to
for thing in lyst:
f.write('{}\n'.format(thing))
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.