From a script, I am getting data like that is given below.
Neha, 30,A
Monika ,22,B
Anni,33,C
I want to convert this data in a given way that is given below.
[{'name':Neha,'age':30,'grade':A},{'name':Monika,'age':22,'grade':B},{'name':Anni,'age':33,'grade':c}]
Can anyone please help me to get the data in this way any suggestion . Thanks in advance
This is your solution
s = """Neha,30,A
Monika,22,B
Anni,33,C"""
# print(s.split('\n'))
l = []
s = s.split('\n')
for i in s:
# print(i)
temp = i.split(',')
# print(temp)
d = {}
d['name'] = temp[0]
d['age'] = temp[1]
d['grade'] = temp[2]
l.append(d)
print(l)
Related
could you help me with this problem? damned for! :p
def exchange(x):
r = requests.get(URL1 + x + URL2)
js = r.json()
df = pd.DataFrame.from_dict(js, orient="index").transpose()
return df
if capture data with next code, after individual append() i have expected answer:
c = exchange("tiendacrypto")
d = exchange("belo")
c.append(d)
but, i don't find the error in the for:
a = []
for i in exchanges:
print(exchange(i))
a = exchange(i)
a.append(a)
The issue here is the reassignment of the a value on line 2 in the for loop.
You need to use a different variable name.
𝚊 = []
𝚏𝚘𝚛 𝚒 𝚒𝚗 𝚎𝚡𝚌𝚑𝚊𝚗𝚐𝚎𝚜:
𝚙𝚛𝚒𝚗𝚝(𝚎𝚡𝚌𝚑𝚊𝚗𝚐𝚎(𝚒))
x = 𝚎𝚡𝚌𝚑𝚊𝚗𝚐𝚎(𝚒)
𝚊.𝚊𝚙𝚙𝚎𝚗𝚍(x)
Notice how we dont now change a in each loop.
You're using a twice.
𝚊 = []
𝚏𝚘𝚛 𝚒 𝚒𝚗 𝚎𝚡𝚌𝚑𝚊𝚗𝚐𝚎𝚜:
𝚙𝚛𝚒𝚗𝚝(𝚎𝚡𝚌𝚑𝚊𝚗𝚐𝚎(𝚒))
𝚊 = 𝚎𝚡𝚌𝚑𝚊𝚗𝚐𝚎(𝚒) # Here is overwritten!
𝚊.𝚊𝚙𝚙𝚎𝚗𝚍(𝚊)#
results = []
𝚏𝚘𝚛 𝚒 𝚒𝚗 𝚎𝚡𝚌𝚑𝚊𝚗𝚐𝚎𝚜:
df = 𝚎𝚡𝚌𝚑𝚊𝚗𝚐𝚎(𝚒)
𝚙𝚛𝚒𝚗𝚝(𝚎𝚡𝚌𝚑𝚊𝚗𝚐𝚎(df))
results.𝚊𝚙𝚙𝚎𝚗𝚍(df)
The .txt file has a string like this:
[[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]]9.5
My goal is to separate that final number from the list and then turn each of them into a list of lists and a float respectively. I've managed to seperate them but they are still strings and I can't convert them...
Here's the code I have:
def string_to_list(file):
for i in os.listdir(path):
if i == file:
openfile = open(path5+'/'+ i, 'r')
values = openfile.read()
p = ']]'
print(values)
print()
bef, sep, after= values.partition(p)
string1 = values.replace(after,'')
print(string1)
print()
print(after)
The output is, using the previous exemple:
[[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]]9.5
[[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]]
9.5
But they are all strings yet.
How can I make this work?
Thank you
ast.literal_eval can do this. json.loads could, as well.
import ast
s = "[[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]]9.5"
i = s.rfind(']')
l = ast.literal_eval(s[:i+1])
o = float(s[i+1:])
print(l, o)
Here is a simple way that only uses list append and loops:
x = list(a[1:len(a)-1]) # remove the outisde brackets
res = []
tmp = []
for e in x:
if e == ']':
res.append(tmp)
tmp = []
continue
if e not in ('[', ',', ' ', ''):
tmp.append(int(e))
You can also use the eval() function after getting the string1 and after values in your code.
myList = eval(string1) #type(myList) will give you list
myFloat = eval(after) #type(myFloat) will give you float
I have a list that holds names of files, some of which are almost identical except for their timestamp string section. The list is in the format of [name-subname-timestamp] for example:
myList = ['name1-001-20211202811.txt', 'name1-001-202112021010.txt', 'name1-002-202112021010.txt', 'name2-002-202112020811.txt']
What I need is a list that holds for every name and subname, the most recent file derived by the timestamp. I have started by creating a list that holds every [name-subname]:
name_subname_list = []
for row in myList:
name_subname_list.append((row.rpartition('-')[0]))
name_subname_list = set(name_subname_list) # {'name1-001', 'name2-002', 'name1-002'}
Not sure if it is the right approach, moreover I am not sure how to continue. Any ideas?
This code is what you asked for:
For each name-subname, you will have the corresponding newest file:
from datetime import datetime as dt
dic = {}
for i in myList:
sp = i.split('-')
name_subname = sp[0]+'-'+sp[1]
mytime = sp[2].split('.')[0]
if name_subname not in dic:
dic[name_subname] = mytime
else:
if dt.strptime(mytime, "%Y%m%d%H%M") > dt.strptime(dic[name_subname], "%Y%m%d%H%M"):
dic[name_subname] = mytime
result = []
for name_subname in dic:
result.append(name_subname+'-'+dic[name_subname]+'.txt')
which out puts resutl to be like:
['name1-001-202112021010.txt',
'name1-002-202112021010.txt',
'name2-002-202112020811.txt']
Try this:
myList = ['name1-001-20211202811.txt', 'name1-001-202112021010.txt', 'name1-002-202112021010.txt', 'name2-002-202112020811.txt']
dic = {}
for name in myList:
parts = name.split('-')
dic.setdefault(parts[0] + '-' + parts[1], []).append(parts[2])
unique_list = []
for key,value in dic.items():
unique_list.append(key + '-' + max(value))
Goal is to replace the second field of csv_line with new_item in an elegant way. This question is different from the topics listed by Rawing because here we are working with a different data structure, though we can use other topics to get inspired.
# Please assume that csv_line has not been imported from a file.
csv_line = 'unknown_item1,unknown_old_item2,unknown_item3'
new_item = 'unknown_new_item2'
goal = 'unknown_item1,unknown_new_item2,unknown_item3'
# Works but error prone. Non-replaced items could be inadvertently swapped.
# In addition, not convenient if string has many fields.
item1, item2, item3 = csv_line.split(',')
result = ','.join([item1, new_item, item3])
print(result) # unknown_item1,unknown_new_item2,unknown_item3
# Less error prone but ugly.
result_list = []
new_item_idx = 1
for i, item in enumerate(csv_line.split(',')):
result_list += [item] if i != new_item_idx else [new_item]
result = ','.join(result_list)
print(result) # unknown_item1,unknown_new_item2,unknown_item3
# Ideal (not-error prone) but not working.
csv_line.split(',')[1] = new_item
print(csv_line) # unknown_item1,unknown_old_item2,unknown_item3
The second item could be replaced using Python's CSV library by making use of io.StringIO() objects. This behave like files but can be read as a string:
import csv
import io
csv_line = 'unknown_item1,unknown_old_item2,unknown_item3'
new_item = 'unknown_new_item2'
row = next(csv.reader(io.StringIO(csv_line)))
row[1] = new_item
output = io.StringIO()
csv.writer(output).writerow(row)
goal = output.getvalue()
print(goal)
This would display goal as:
unknown_item1,unknown_new_item2,unknown_item3
l = csv_line.split(',')
l[1] = new_item
csv_line = ','.join(l)
In the line csv_line.split(',')[1] = new_item, you do not alter the csv_line variable at all. You need to assign the new list created with .split() to a variable before you can change the elements within it:
new_csv = csv_line.split(',')
new_csv[1] = new_item
print(','.join(new_csv))
This seems the most pythonic:
csv_line = 'unknown_item1,old_item2,unknown_item3'
old_index = 1
new_item = 'new_item2'
goal = 'unknown_item1,new_item2,unknown_item3'
items = csv_line.split(',')
items[old_index] = new_item
print(','.join(items))
print(goal)
Output:
unknown_item1,new_item2,unknown_item3
unknown_item1,new_item2,unknown_item3
I've written some code that can parse a string into tuples as such:
s = '30M3I5X'
l = []
num = ""
for c in s:
if c in '0123456789':
num = num + c
print(num)
else:
l.append([int(num), c])
num = ""
print(l)
I.e.;
'30M3I5X'
becomes
[[30, 'M'], [3, 'I'], [5, 'X']]
That part works just fine. I'm struggling now, however, with figuring out how to get the values from the first column of a tab-separated-value file to become my new 's'. I.e.; for a file that looks like:
# File Example #
30M3I45M2I20M I:AAC-I:TC
50M3X35M2I20M X:TCC-I:AG
There would somehow be a loop incorporated to take only the first column, producing
[[30, 'M'],[3, 'I'],[45, 'M'],[2, 'I'],[20, 'M']]
[[50, 'M'],[3, 'X'],[35, 'M'],[2, 'I'],[20, 'M']]
without having to use
import csv
Or any other module.
Thanks so much!
Just open the path to the file and iterate through the records?
def fx(s):
l=[]
num=""
for c in s:
if c in '0123456789':
num=num+c
print(num)
else:
l.append([int(num), c])
num=""
return l
with open(fp) as f:
for record in f:
s, _ = record.split('\t')
l = fx(s)
# process l here ...
The following code would serve your purpose
rows = ['30M3I45M2I20M I:AAC-I:TC', '30M3I45M2I20M I:AAC-I:TC']
for row in rows:
words = row.split(' ')
print(words[0])
l = []
num = ""
for c in words[0]:
if c in '0123456789':
num = num + c
else:
l.append([int(num), c])
print(l)
Change row.split(' ') to ('\t') or any other seperator as per the need
something like this should do what you're looking for.
filename = r'\path\to\your\file.txt'
with open(filename,'r') as input:
for row in input:
elements = row.split()
# processing goes here
elements[0] contains the string that is the first column of data in the file.
Edit:
to end up with a list of the lists of processed data:
result = []
filename = r'\path\to\your\file.txt'
with open(filename,'r') as input:
for row in input:
elements = row.split()
# processing goes here
result.append(l) # l is the result of your processing
So this is what ended up working for me--took bits and pieces from everyone, thank you all!
Note: I know it's a bit verbose, but since I'm new, it helps me keep track of everything :)
#Defining the parser function
def col1parser(col1):
l = []
num = ""
for c in col1:
if c in '0123456789':
num = num + c
else:
l.append([int(num), c])
num = ""
print(l)
#Open file, run function on column1
filename = r'filepath.txt'
with open(filename,'r') as input:
for row in input:
elements = row.split()
col1 = elements[0]
l = col1parser(col1)