I have a csv file. I want to create dictionary from this data.I should not pandas. Data looks like this:
I do this. But size of the numbers is not same. How can I create a dictionary?
filename=" data.dat"
file=open(filename, encoding="latin-1").read().split(' , ')
dictt={}
for row in file:
dictt[row[0]] = {‘values’, row[1]}
I have a file as above. First, I need to create a dict. After that, I will print the daily number of unique measurements in desending order according to date.
Final Expected result:
Hi
That will do what you want
with open("./test.txt") as myFile:
formattedData = dict()
for line in myFile:
try:
date , numbers = line.split(' , ')
numbers = numbers.replace("\n","")
numbers = numbers.split(',')
formattedData[date] = len(list(set(numbers)))
except:
date = line
formattedData[date] = 0
print(formattedData)
Firstly, do not call your variables 'list' as that is a python keyword and it will cause confusion. I can't reproduce this because I don't have the file, but I think changing the line in the for loop to this should work.
newvariablename[row[0]] = row[1]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.