Convert JSON values into a string - python
r = '{"available_sizes":[{"id":104682,"name":"40","preorder_only":false},{"id":104683,"name":"41","preorder_only":false},{"id":104684,"name":"42","preorder_only":false},{"id":104685,"name":"43","preorder_only":false},{"id":104687,"name":"45","preorder_only":false}]}'
data = json.loads(r)
for element in data:
for value in data['available_sizes']:
print(value['name'])
At the moment this prints out the following:
40
41
42
43
45
How would I then use this data as a string? Below is the desired output.
Available sizes are 40, 41, 41, 43, 45
Your outermost loop is superfluous, since you only have a single key to iterate over.
Iterate over data, append your numbers to a list, and then call str.join at the end to efficiently join your strings together.
nums = []
for v in data['available_sizes']:
nums.append(str(v['name'])) # v['name'] if it is already string
print(f'Available sizes are {', '.join(nums)}')
You can rewrite the loop using a list comprehension -
num_str = ', '.join([v['name'] for v in data['available_sizes']])
print(f'Available sizes are {num_str}')
For a primer on JSON data traversals, I recommend looking at this answer.
Use a for-comprehension to extract the size names, and then str.join() to add the comma separator:
import json
r = '{"available_sizes":[{"id":104682,"name":"40","preorder_only":false},{"id":104683,"name":"41","preorder_only":false},{"id":104684,"name":"42","preorder_only":false},{"id":104685,"name":"43","preorder_only":false},{"id":104687,"name":"45","preorder_only":false}]}'
data = json.loads(r)
# Extract the size names from the list of available sizes
size_names = [size_entry["name"] for size_entry in data["available_sizes"]]
# Join the sizes names as strings using a comma separator and print
sizes_string = ", ".join(size_names)
print("Available sizes are: " + sizes_string)
Do something like that this is the out put that you want
import json
r = '{"available_sizes":[{"id":104682,"name":"40","preorder_only":false},{"id":104683,"name":"41","preorder_only":false},{"id":104684,"name":"42","preorder_only":false},{"id":104685,"name":"43","preorder_only":false},{"id":104687,"name":"45","preorder_only":false}]}'
data = json.loads(r)
var = []
for element in data:
for value in data['available_sizes']:
var.append(value['name'])
print( 'Availble size are %s' %(', '.join(var)))
Related
modify a string python
I have a csv file structured in the following way: num mut 36 L 45 P ... where num indicates the position of a mutation and mut indicates the mutation. I have to modify at the position num with the letter mut a string. I wrote the following code in python: import pandas as pd import os df = pd.read_csv(r'file.csv') df_tmp=df.astype(str) df_tmp["folder"]=df_tmp["num"]+df_tmp["mut"] #add a third column f = open("sequence.txt", 'r') content = f.read() for i in range(len(df)): num=df_tmp.num.loc[[i]]-13 num=num.astype(int) prev=num-1 prev=prev.astype(int) mut=df_tmp.mut.loc[[i]] mut=mut.astype(str) new="".join((content[:prev],mut,content[num:])) #this should modify the file But it returns me TypeError: slice indices must be integers or None or have an __index__ method How can I solve? Edit: maybe it is more clear what I want to do. I have to insert only the first mutation in my sequence, save it to a file, copy the file in a folder that is named as the third column (that I added in the code), make the same thing with the second mutation, then the third and so on. But I have to insert only one mutation at time.
multiple mutations: IIUC, you'd be better off pandas, convert your dataframe to dictionary, iterate and join: # input DataFrame df = pd.DataFrame({'num': [36, 45], 'mut': ['L', 'P']}) # input string string = '-'*50 # '--------------------------------------------------' # get the positions to modify pos = df.set_index('num')['mut'].to_dict() # {36: 'L', 45: 'P'} # iterate over the string, replace hte characters if in the dictionary # NB. define start=1 if you want the first position to be 1 new_string = ''.join([pos.get(i, c) for i,c in enumerate(string, start=0)]) # '------------------------------------L--------P----' single mutations: string = '-'*50 # '--------------------------------------------------' for idx, r in df.iterrows(): new_string = string[:r['num']-1]+r['mut']+string[r['num']:] # or # new_string = ''.join([string[:r['num']-1], r['mut'], string[r['num']:]]) with open(f'file_{idx}.txt', 'w') as f: f.write(new_string) output: file_0.txt -----------------------------------L-------------- file_1.txt --------------------------------------------P-----
I tried your code with a sample file.csv and an empty sequence.txt file, in your code first line from for loop num=df_tmp.num.loc[[i]]-13 #gives an error since the num in that location is str, to correct that: num=df_tmp.num.loc[[i]].astype(int)-13 # I used astype to convert it into int first After this the next error is in last line , the slice indices type error, This is due to the fact that , the resulting prev and num you use to slice the content variable is not a int, to get the int value add a [0] to it in this way: content="".join((content[:prev[0]],mut,content[num[0]:])) There shouldn't be an error now.
How to turn a list containing strings into a list containing integers (Python)
I am optimizing PyRay (https://github.com/oscr/PyRay) to be a usable Python ray-casting engine, and I am working on a feature that takes a text file and turns it into a list (PyRay uses as a map). But when I use the file as a list, it turns the contents into strings, therefore not usable by PyRay. So my question is: How do I convert a list of strings into integers? Here is my code so far. (I commented the actual code so I can test this) print("What map file to open?") mapopen = input(">") mapload = open(mapopen, "r") worldMap = [line.split(',') for line in mapload.readlines()] print(worldMap) The map file: 1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,2,0,0,3,0,0,0,0,0,0,0,2,3,2,3,0,0,2, 2,0,3,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 2,3,1,0,0,2,0,0,0,2,3,2,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,1,0,1,0,0,1,2,0,0,0,2, 2,0,0,0,0,0,0,0,0,2,0,2,0,0,2,1,0,0,0,1, 1,0,0,0,0,0,0,0,0,1,3,1,0,0,0,0,0,0,0,2, 2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,2,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 2,0,3,0,0,2,0,0,0,0,0,0,0,2,3,2,1,2,0,1, 1,0,0,0,0,3,0,0,0,0,0,0,0,1,0,0,2,0,0,2, 2,3,1,0,0,2,0,0,2,1,3,2,0,2,0,0,3,0,3,1, 1,0,0,0,0,0,0,0,0,3,0,0,0,1,0,0,2,0,0,2, 2,0,0,0,0,0,0,0,0,2,0,0,0,2,3,0,1,2,0,1, 1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,3,0,2, 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,1, 2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1, Please help me, I have been searching all about and I can't find anything.
try this: Did you want a list of lists? or just one big list? with open(filename, "r") as txtr: data = txtr.read() data = txtr.split("/n") # split into list of strings data = [ list(map(int, x.split(","))) for x in data] fourth line splits string into list by removing comma, then appliea int() on each element then turns it into a list. It does this for every element in data. I hope it helps. Here is for just one large list. with open(filename, "r") as txtr: data = txtr.readlines() # remove empty lines in your file! data = ",".join(data) # turns it into a large string data = data.split(",") # now you have a list of strings data = list(map(int, data)) # applies int() to each element in data.
Look into the map built-in function in python. L=['1', '2', '3'] map = map(int, L) for el in map: print(el) >>> 1 ... 2 ... 3
As per you question, please find below a way you can change list of strings to list of integers (or integers if you use list index to get the integer value). Hope this helps. myStrList = ["1","2","\n","3"] global myNewIntList myNewIntList = [] for x in myStrList: if(x != "\n"): y = int(x) myNewIntList.append(y) print(myNewIntList)
Tupling floats into pairs and adding them to a python dictionary
I have a text file of countries and some describing coordinates, with the following format: Country 57.7934235704;24.3128625831 58.3834133979;24.42892785 58.2573745795;24.0611983579 58.6127534044;23.4265600929 And i'm having trouble converting the file into a python dictionary with country as the key, and the values as list of lists of float-tuples, like so: [[(57.7934235704, 24.3128625831), (58.3834133979, 24.42892785), (58.2573745795, 24.0611983579), (58.6127534044, 23.4265600929)]] I've managed to end up with the following code, which in my understanding manages to add the country as a key, and floats the coordinates individually, so what's missing is a way to tuple the floats in pairs, and add them to their corresponding country. def read_country_file(filename): with open(filename) as file: dict = {} for line in file: line = line.rstrip().split(' ') for element in line: if re.match('^[A-Z]', element): #if the line starts with a letter make it a key country = (element[0:]) dict[country] = country elif re.match('^[-0-9;. ]', element): #if the line starts with a number make it a value element = element.split(';') for i in element: flo = float(i) #MISSING: Tuple floats in pairs and add them to the dictionary return dict If I lookup a country in this dictionary, it will find the country/key correctly, but it has no values attached. And if I type-test my "flo" value it's a float, so i have a feeling I'm almost there.
Let's use tuple comprehension: element = tuple(float(i) for i in element.split(';')) Additionally, my solution for your problem: import re text = ['Vietnam', '57.7934235704;24.3128625831 58.3834133979;24.42892785 58.2573745795;24.0611983579 58.6127534044;23.4265600929'] def get_tuples_of_float(string): return [tuple(map(float, j)) for j in re.findall('([\d.]+);([\d.]+)', string)] it = iter(text) output = { i : get_tuples_of_float(next(it)) for i in it if re.match('^[A-Z]', i)}
You can use re.findall: import re s = """ 57.7934235704;24.3128625831 58.3834133979;24.42892785 58.2573745795;24.0611983579 58.6127534044;23.4265600929 """ new_data = map(float, re.findall('[\d\.]+', s)) final_data = {new_data[i]:new_data[i+1] for i in range(0, len(new_data), 2)} Output: {58.6127534044: 23.4265600929, 58.2573745795: 24.0611983579, 58.3834133979: 24.42892785, 57.7934235704: 24.3128625831}
Why don't you first split each line of text based on spaces and then the array that comes out from it, you then split each individual coordinate pair based on the semicolons that are common to them then you can now add everything to the country key on the dictionary.
Python adding to the list
I have to strip whitespace for extracted strings, one string at a time for which I'm using split(). The split() function returns list after removing white spaces. I want to store this in my own dynamic list since I have to aggregate all of the strings. The snippet of my code: while rec_id = "ffff" output = procs.run_cmd("get sensor info", command) sdr_li = [] if output: byte_str = output[0] str_1 = byte_str.split(' ') for byte in str_1: sdr_li.append(byte) rec_id = get_rec_id() Output = ['23 0a 06 01 52 2D 12'] str_1 = ['23','0a','06','01','52','2D','12'] This does not look very elegant, transferring from one list to another. Is there another way to achieve this.
list.extend(): sdr_li.extend(str_1)
str.split() returns you a list so just add your list's items to the main list. Use extend https://docs.python.org/2/tutorial/datastructures.html so rewriting your data into something legible and properly indented you'd get: my_list = list while rec_id = "ffff" output = procs.run_cmd("get sensor info", command) if output: result_string = output[0] # extend my_list with the list resulting from the whitespace # seperated tokens of the output my_list.extend( result_string.split() ) pass # end if rec_id = get_rec_id() ... pass # end while
Reading in data from file using regex in python
I have a data file with tons of data like: {"Passenger Quarters",27.`,"Cardassian","not injured"},{"Passenger Quarters",9.`,"Cardassian","injured"},{"Passenger Quarters",32.`,"Romulan","not injured"},{"Bridge","Unknown","Romulan","not injured"} I want to read in the data and save it in a list. I am having trouble getting the exact right code to exact the data between the { }. I don't want the quotes and the ` after the numbers. Also, data is not separated by line so how do I tell re.search where to begin looking for the next set of data?
At first glance, you can break this data into chunks by splitting it on the string },{: chunks = data.split('},{') chunks[0] = chunks[0][1:] # first chunk started with '{' chunks[-1] = chunks[-1][:-1] # last chunk ended with '}' Now you have chunks like "Passenger Quarters",27.`,"Cardassian","not injured" and you can apply a regular expression to them.
You should do this in two passes. One to get the list of items and one to get the contents of each item: import re from pprint import pprint data = '{"Passenger Quarters",27.`,"Cardassian","not injured"},{"Passenger Quarters",9.`,"Cardassian","injured"},{"Passenger Quarters",32.`,"Romulan","not injured"},{"Bridge","Unknown","Romulan","not injured"}' # This splits up the data into items where each item is the # contents inside a pair of braces item_pattern = re.compile("{([^}]+)}") # This plits up each item into it's parts. Either matching a string # inside quotation marks or a number followed by some garbage contents_pattern = re.compile('(?:"([^"]+)"|([0-9]+)[^,]+),?') rows = [] for item in item_pattern.findall(data): row = [] for content in contents_pattern.findall(item): if content[1]: # Number matched, treat it as one row.append(int(content[1])) else: # Number not matched, use the string (even if empty) row.append(content[0]) rows.append(row) pprint(rows)
The following will produce a list of lists, where each list is an individual record. import re data = '{"Passenger Quarters",27.`,"Cardassian","not injured"},{"Passenger Quarters",9.`,"Cardassian","injured"},{"Pssenger Quarters",32.`,"Romulan","not injured"},{"Bridge","Unknown","Romulan","not injured"}' # remove characters we don't want and split into individual fields badchars = ['{','}','`','.','"'] newdata = data.translate(None, ''.join(badchars)) fields = newdata.split(',') # Assemble groups of 4 fields into separate lists and append # to the parent list. Obvious weakness here is if there are # records that contain something other than 4 fields records = [] myrecord = [] recordcount = 1 for field in fields: myrecord.append(field) recordcount = recordcount + 1 if (recordcount > 4): records.append(myrecord) myrecord = [] recordcount = 1 for record in records: print record Output: ['Passenger Quarters', '27', 'Cardassian', 'not injured'] ['Passenger Quarters', '9', 'Cardassian', 'injured'] ['Pssenger Quarters', '32', 'Romulan', 'not injured'] ['Bridge', 'Unknown', 'Romulan', 'not injured']