Python list write to CSV without the square brackets - python

I have this main function:
def main():
subprocess.call("cls", shell=True)
ipList,hostList,manfList,masterList,temp = [],[],[],[],[]
ipList,hostList,manfList, = getIPs(),getHosts(),getManfs()
entries = len(hostList)
i = 0
for i in xrange(i, entries):
temp = [[hostList[i]],[manfList[i]],[ipList[i]]]
masterList.append(temp)
with open("output.csv", "wb") as f:
writer = csv.writer(f, delimiter=',')
writer.writerows(masterList)
My current output is that it successfully writes to CSV but my objective is to remove the square brackets.
I tried using .join() method however I understand that it only takes single lists and not nested lists.
How can I achieve this given that I'm using a 3 dimensional list? Note, I intend to add more columns of data in the future.
Edit:
My current output for 1 row is similar to:
['Name1,'] ['Brand,'] ['1.1.1.1,']
I would like it to be:
Name1, Brand, 1.1.1.1,

Try to remove bracket for values in temp while creating masterList, because it will be nested list. So, the code should be:
def main():
subprocess.call("cls", shell=True)
ipList,hostList,manfList,masterList,temp = [],[],[],[],[]
ipList,hostList,manfList, = getIPs(),getHosts(),getManfs()
entries = len(hostList)
i = 0
for i in xrange(i, entries):
temp = [hostList[i], manfList[i], ipList[i]]
masterList.append(temp)
with open("output.csv", "wb") as f:
writer = csv.writer(f, delimiter=',')
writer.writerows(masterList)

What you could do is strip a string of the data maybe?
import string
writer.writerows(str(masterList).translate(string.maketrans('', ''), '[]\'')
E.g.
>>> import string
>>> temp = [['1.1.1'], ['Name1'], ['123']]
>>> str(temp).translate(string.maketrans('', ''), '[]\'')
'1.1.1, Name1, 123'
In Python 3.6:
>>> temp = [['1.1.1'], ['Name1'], ['123']]
>>> str(temp).translate({ord('['): '', ord(']'): '', ord('\''): ''})
'1.1.1, Name1, 123'

Try to change this:
temp = [[hostList[i]],[manfList[i]],[ipList[i]]]
to this:
temp = [hostList[i],manfList[i],ipList[i]]

I agree with the answers above, about the brackets removal, however if this is crucial to you for some reason, here is a function that takes a list as an input and returns you a csv row acceptable list.
def output_list(masterList):
output = []
for item in masterList:
if isinstance(item,list): #if item is a list
for i in output_list(item): #call this function on it and append its each value separately. If it has more lists in it this function will call itself again
output.append(i)
else:
output.append(item)
return output
You can use it in the line masterList.append(temp) as masterList.append(output_list(temp)), or even like this:
#in the end
with open("output.csv", "wb") as f:
writer = csv.writer(f, delimiter=',')
for i in masterList:
writer.writerow(output_list(i))

Related

Reading values from file and storing them in dictionary

I want to write a function read_file() to read the values in the file and store the data in the a dictionary. The dictionary will look something like this:
product = {'d01':['pencil', 5], 'd02':['highlighter', 7], 'd03':['sharpener', 10]....}
What the items in the file looks like:
input:
d={}
file=r"C:\Users\Public\Documents\Folder\products.dat"
with open(file,'r') as f:
for items in f:
print(items)
results:
d01,pencil,5
d02,highlighter,7
d03,sharpener, 10
d04,pen,3
Here are my codes:
def read_file():
d={}
file=r"C:\Users\Public\Documents\Folder\products.dat"
with open(file,'r') as f:
for items in f:
stuff = items.split(",")
quantity = int(stuff[2].rstrip())
a = stuff[0]
b = [stuff[1], quantity]
d = {a:b}
print(d)
read_file()
Currently results I got:
{'d01': ['pencil', 5]}
{'d02': ['highlighter', 7]}
{'d03': ['sharpener', 10]}
{'d04': ['pen', 3]}
How do I achieve the above results?
Don't create a new dictionary for each line, add an element to the same dictonary.
Change
d = {a:b}
to
d[a] = b
And put print(d) after the loop is done, not inside the loop.
To read and parse a csv file into a dictionary of lists, using the first item on each line as a key and the remaining items on each line as a value list:
import csv
def parse(csvfilename):
dic = {}
with open(csvfilename, "r") as csvfile
csvreader = csv.reader(csvfile, skipinitialspace=True)
for row in csvreader:
table[row[0]] = row[1:]
return dic

Print out dictionary from file

E;Z;X;Y
I tried
dl= defaultdict(list)
for line in file:
line = line.strip().split(';')
for x in line:
dl[line[0]].append(line[1:4])
dl=dict(dl)
print (votep)
It print out too many results. I have an init that reads the file.
What ways can I edit to make it work?
The csv module could be really handy here, just use a semicolon as your delimiter and a simple dict comprehension will suffice:
with open('filename.txt') as file:
reader = csv.reader(file, delimiter=';')
votep = {k: vals for k, *vals in reader}
print(votep)
Without using csv you can just use str.split:
with open('filename.txt') as file:
votep = {k: vals for k, *vals in (s.split(';') for s in file)}
print(votep)
Further simplified without the comprehension this would look as follows:
votep = {}
for line in file:
key, *vals = line.split(';')
votep[key] = vals
And FYI, key, *vals = line.strip(';') is just multiple variable assignment coupled with iterable unpacking. The star just means put whatever’s left in the iterable into vals after assigning the first value to key.
if you read file in list object, there is a simple function to iterate over and convert it to dictionary you expect:
a = [
'A;X;Y;Z',
'B;Y;Z;X',
'C;Y;Z;X',
'D;Z;X;Y',
'E;Z;X;Y',
]
def vp(a):
dl = {}
for i in a:
split_keys = i.split(';')
dl[split_keys[0]] = split_keys[1:]
print(dl)

Converting a list to json in python

Here is the code, I have a list, which I want to convert to JSON with dynamic keys.
>>> print (list) #list
['a', 'b', 'c', 'd']
>>> outfile = open('c:\\users\\fawads\desktop\csd\\Test44.json','w')#writing data to file
>>> for entry in list:
... data={'key'+str(i):entry}
... i+=1
... json.dump(data,outfile)
...
>>> outfile.close()
The result is as following:
{"key0": "a"}{"key1": "b"}{"key2": "c"}{"key3": "d"}
Which is not valid json.
Enumerate your list (which you should not call list, by the way, you will shadow the built in list):
>>> import json
>>> lst = ['a', 'b', 'c', 'd']
>>> jso = {'key{}'.format(k):v for k, v in enumerate(lst)}
>>> json.dumps(jso)
'{"key3": "d", "key2": "c", "key1": "b", "key0": "a"}'
data = []
for entry in lst:
data.append({'key'+str(lst.index(entry)):entry})
json.dump(data, outfile)
As a minimal change which I originally posted in a comment:
outfile = open('c:\\users\\fawads\desktop\csd\\Test44.json','w')#writing data to file
all_data = [] #keep a list of all the entries
i = 0
for entry in list:
data={'key'+str(i):entry}
i+=1
all_data.append(data) #add the data to the list
json.dump(all_data,outfile) #write the list to the file
outfile.close()
calling json.dump on the same file multiple times is very rarely useful as it creates multiple segments of json data that needs to be seperated in order to be parsed, it makes much more sense to only call it once when you are done constructing the data.
I'd also like to suggest you use enumerate to handle the i variable as well as using a with statement to deal wit the file IO:
all_data = [] #keep a list of all the entries
for i,entry in enumerate(list):
data={'key'+str(i):entry}
all_data.append(data)
with open('c:\\users\\fawads\desktop\csd\\Test44.json','w') as outfile:
json.dump(all_data,outfile)
#file is automatically closed at the end of the with block (even if there is an e
The loop could be shorted even further with list comprehension:
all_data = [{'key'+str(i):entry}
for i,entry in enumerate(list)]
Which (if you really want) could be put directly into the json.dump:
with open('c:\\users\\fawads\desktop\csd\\Test44.json','w') as outfile:
json.dump([{'key'+str(i):entry}
for i,entry in enumerate(list)],
outfile)
although then you start to lose readability so I don't recommend going that far.
Here is what you need to do:
mydict = {}
i = 0
for entry in list:
dict_key = "key" + str(i)
mydict[dict_key] = entry
i = i + 1
json.dump(mydict, outfile)
Currently you are creating a new dict entry in every iteration of the loop , hence the result is not a valid json.

Miminum of each list in a list of lists

I was wondering if it was possible to get the minimum value of each list in a list of lists.
import csv
data=[]
file=input ("Enter file name: ")
with open(file,"r") as f:
reader = csv.reader(f, delimiter=';')
for row in reader:
data.append(row)
print(data)
Output:
[['13.25', '12.97', '13.12', '13.47', '13.44', '13.09', '12.86', '12.78', '12.91', '12.93', '12.91', '13.11'], ['12.92', '13.42', '13.58', '13.7', '13.62', '13.7', '13.31', '12.86', '12.59', '12.81', '13.46', '12.9'], ['13.39', '13.5', '13.29', '13.26', '13.38', '13.45', '13.46', '11.95', '', '12.57', '13.22', '12.88'], ['12.48', '13.76', '13.7', '13.77', '13.08', '13.48', '13.25', '12.31', '12.56', '12.56', '12.95', '13.38'], ['12.52', '14.07', '14.46', '14.13', '13.98', '14.07', '13.92', '12.7', '13.01', '12.79', '13', '13.13']]
The minimum value of a list of lists of numbers can be achieved with a simple call to map:
numbers = [[1,3,6], [6,7,2], [0,-4]]
minimum_numbers = map(min, numbers)
print minimum_numbers
# This prints: [1, 2, -4]
This will return a list where min was called for each element in the "outer list" numbers.
Python has a min function that can be called on lists to get the lowest value. You can loop through your data list and call min() on each list contained.
for list in data:
print(min(list))
Given your output this would return:
12.78
12.59
12.31
12.52
The third one is blank because the array has an empty value in it.
If you want to remove empty strings you can use filter()
for list in data:
list = filter(None, list)
print(min(list))
This outputs
12.78
12.59
11.95
12.31
12.52
Also I noticed all the values are floats, you can also convert them before checking min value so you're not comparing strings.
for list in data:
list = filter(None, list)
list = [float(i) for i in list]
print(min(list))
First you'll need to convert each of the items to floats. Do this when you read the data in:
import csv
data=[]
file=input ("Enter file name: ")
with open(file,"r") as f:
reader = csv.reader(f, delimiter=';')
for row in reader:
data.append([float(x) for x in row if x]) # converts values to floats, ignores empty ones
print(data)
Once your done with that, just call min() on each list:
>>> [min(x) for x in data]
[12.78, 12.59, 11.95, 12.31, 12.52]
You can shorten your reading loop to this:
>>> with open(file, "r") as f:
... reader = csv.reader(f, delimiter=";")
... data = [[float(x) for x in row] for row in reader]
>>> print([min(x) for x in data])
I think that you should use Emil Vikström's answer, but since you have control over your loading code you can prevent unnecessary storing of data and simply do:
with open(file,"r") as f:
reader = csv.reader(f, delimiter=';')
for row in reader:
data.append(min(map(float, row)))
I would use the map function...
listOfLists = [ [2, 3, 1], [5, 7, 8], .... ]
minValues = map(lambda x: min(x), listOfLists)
Result:
minValues = [1, 5, ...]

How to convert values to float and assign them to a dictionary in Python?

I am trying to read content from a CSV file and store them in a dictionary.
Each row of my CSV file is formatted as:
'Node_001', '0.0067', '0.2456', '0.7896', ......
The first element will be used as key in dictionary, while the rest part are values.
Since these values are generated by equations in excel, I don't think there are anything wrong with the format itself.
Here is my code:
with open(path, "rb") as file:
reader = csv.reader(file)
my_dictionary = dict()
for row in reader:
node_id = row[0]
temp_values = row[1:]
[float(x) for x in temp_values]
my_dictionary[node_id] = temp_values
print isinstance(temp_values[0], float)
I print the first element of the numeric part of my rows to exam if they are converted to float. However, all I got is False.
So, may I know what is wrong with my code?
Thanks.
The line [float(x) for x in temp_values] does not modify temp_values but creates a new list. you have to reassign it like:
with open(path, "rb") as file:
reader = csv.reader(file)
my_dictionary = dict()
for row in reader:
node_id = row[0]
temp_values = row[1:]
temp_values = [float(x) for x in temp_values]
my_dictionary[node_id] = temp_values
print isinstance(temp_values[0], float)
This chunk of code:
for row in reader:
node_id = row[0]
temp_values = row[1:]
[float(x) for x in temp_values]
my_dictionary[node_id] = temp_values
print isinstance(temp_values[0], float)
creates a list of float values with this line:
[float(x) for x in temp_values]
...but since it's not assigned to anything, it goes away immediately.
changing that line to
temp_values = [float(x) for x in temp_values]
creates the converted list and assigns it to temp_values so the rest of your code can use those values.
Try this for a change, assuming you only have unique keys in your file:
with open(path, 'r') as f:
reader = csv.reader(f)
d = {r[0]:map(float, r[1:]) for r in reader}
print(d)
You can also stick with a list comprehension with this:
with open(path, 'r') as f:
reader = csv.reader(f)
d = {r[0]: [float(i) for i in r[1:]] for r in reader}
You are not saving the conversion:
temp_values = [float(x) for x in temp_values]
If you replace your list comprehension with this one, your code should work.

Categories