How to convert a string of strings into a list? - python

I have the string
"Abarth", "AC", "Aixam", "Ak", "Alfa%20Romeo", "Alpine", "Ariel", "Aston%20Martin", "Audi", "Austin", "Bac", "Beauford", "Bentley", "BMW", "Bristol", "Bugatti", "Buick", "Cadillac", "Caterham", "Chesil", "Chevrolet", "Chrysler", "Citroen", "Corvette", "Cupra", "Custom%20Vehicle", "Dacia", "Daewoo", "Daihatsu", "Daimler", "Datsun", "DAX", "Dodge", "DS%20AUTOMOBILES", "Ferrari", "Fiat", "Ford", "GMC", "Great%20Wall", "Holden", "Honda", "Humber", "Hummer", "Hyundai", "Infiniti", "Isuzu", "Iveco", "Jaguar", "Jeep", "Jensen", "Kia", "Koenigsegg", "KTM", "Lamborghini", "Lancia", "Land%20Rover", "Levc", "Lexus", "Leyland", "Lincoln", "Lotus", "Maserati", "Maybach", "Mazda", "McLaren", "Mercedes-Benz", "MG", "MINI", "Mitsubishi", "Morgan", "Morris", "Nissan", "Noble", "Opel", "Perodua", "Peugeot", "Pilgrim", "Plymouth", "Pontiac", "Porsche", "Proton", "Radical", "Raw", "Reliant", "Renault", "Replica", "Riley", "Robin%20Hood", "Rolls-Royce", "Rover", "Saab", "SEAT", "SKODA", "Smart", "Ssangyong", "Standard", "Subaru", "Sunbeam", "Suzuki", "Tesla", "Tiger", "Toyota", "Triumph", "TVR", "Vauxhall", "Volkswagen", "Volvo", "Westfield", "Yamaha", "Zenos"
which is stored by the variable "list_of_car_makes" in my python project
but I want to convert the string into a list, how would I do that?
when I use print(list_of_car_makes[0]) it prints the first character in the string, the ",
what i'd like to be able to print is Abarth when using that print function.

Okay you have other weird characters in there what I'm going to ignore (%20 is a space, but you can work out how to fix that)
Your issue to the letter would be solved by:
your_string_here.replace('"', '').split(',')

Try this:
makes = []
word = ""
for c in list_of_car_makes:
if c in " ,":
continue
if c == '"':
if word:
makes.append(word)
word = ""
continue
word += c
print(makes)
Output:
['Abarth', 'AC', 'Aixam', 'Ak', 'Alfa%20Romeo', 'Alpine', 'Ariel', 'Aston%20Martin', 'Audi', 'Austin', 'Bac', 'Beauford', 'Bentley', 'BMW', 'Bristol', 'Bugatti', 'Buick', 'Cadillac', 'Caterham', 'Chesil', 'Chevrolet', 'Chrysler', 'Citroen', 'Corvette', 'Cupra', 'Custom%20Vehicle', 'Dacia', 'Daewoo', 'Daihatsu', 'Daimler', 'Datsun', 'DAX', 'Dodge', 'DS%20AUTOMOBILES', 'Ferrari', 'Fiat', 'Ford', 'GMC', 'Great%20Wall', 'Holden', 'Honda', 'Humber', 'Hummer', 'Hyundai', 'Infiniti', 'Isuzu', 'Iveco', 'Jaguar', 'Jeep', 'Jensen', 'Kia', 'Koenigsegg', 'KTM', 'Lamborghini', 'Lancia', 'Land%20Rover', 'Levc', 'Lexus', 'Leyland', 'Lincoln', 'Lotus', 'Maserati', 'Maybach', 'Mazda', 'McLaren', 'Mercedes-Benz', 'MG', 'MINI', 'Mitsubishi', 'Morgan', 'Morris', 'Nissan', 'Noble', 'Opel', 'Perodua', 'Peugeot', 'Pilgrim', 'Plymouth', 'Pontiac', 'Porsche', 'Proton', 'Radical', 'Raw', 'Reliant', 'Renault', 'Replica', 'Riley', 'Robin%20Hood', 'Rolls-Royce', 'Rover', 'Saab', 'SEAT', 'SKODA', 'Smart', 'Ssangyong', 'Standard', 'Subaru', 'Sunbeam', 'Suzuki', 'Tesla', 'Tiger', 'Toyota', 'Triumph', 'TVR', 'Vauxhall', 'Volkswagen', 'Volvo', 'Westfield', 'Yamaha', 'Zenos']
To remove the %20 (and other codes, add this bit of code):
for j in range(len(makes)):
while "%" in makes[j]:
i = makes[j].find("%")
s = int(makes[j][i+1:i+3], base=16)
m = list(makes[j])
del m[i:i+3]
m.insert(i, chr(s))
makes[j] = ''.join(m)
Output with this code:
['Abarth', 'AC', 'Aixam', 'Ak', 'Alfa Romeo', 'Alpine', 'Ariel', 'Aston Martin', 'Audi', 'Austin', 'Bac', 'Beauford', 'Bentley', 'BMW', 'Bristol', 'Bugatti', 'Buick', 'Cadillac', 'Caterham', 'Chesil', 'Chevrolet', 'Chrysler', 'Citroen', 'Corvette', 'Cupra', 'Custom Vehicle', 'Dacia', 'Daewoo', 'Daihatsu', 'Daimler', 'Datsun', 'DAX', 'Dodge', 'DS AUTOMOBILES', 'Ferrari', 'Fiat', 'Ford', 'GMC', 'Great Wall', 'Holden', 'Honda', 'Humber', 'Hummer', 'Hyundai', 'Infiniti', 'Isuzu', 'Iveco', 'Jaguar', 'Jeep', 'Jensen', 'Kia', 'Koenigsegg', 'KTM', 'Lamborghini', 'Lancia', 'Land Rover', 'Levc', 'Lexus', 'Leyland', 'Lincoln', 'Lotus', 'Maserati', 'Maybach', 'Mazda', 'McLaren', 'Mercedes-Benz', 'MG', 'MINI', 'Mitsubishi', 'Morgan', 'Morris', 'Nissan', 'Noble', 'Opel', 'Perodua', 'Peugeot', 'Pilgrim', 'Plymouth', 'Pontiac', 'Porsche', 'Proton', 'Radical', 'Raw', 'Reliant', 'Renault', 'Replica', 'Riley', 'Robin Hood', 'Rolls-Royce', 'Rover', 'Saab', 'SEAT', 'SKODA', 'Smart', 'Ssangyong', 'Standard', 'Subaru', 'Sunbeam', 'Suzuki', 'Tesla', 'Tiger', 'Toyota', 'Triumph', 'TVR', 'Vauxhall', 'Volkswagen', 'Volvo', 'Westfield', 'Yamaha', 'Zenos']

I would suggest splitting by your delimiter then using str.split as you can provide it with characters to trim from the ends of a string.
[s.strip('"') for s in your_string.split(', ')]
You can even replace %20 with a space this way.
[s.strip('"').replace('%20', ' ') for s in your_string.split(', ')]
Try it here!

Related

Creating undirected unweighted graph from dictionary containing neighborhood relationship

I have a Python dictionary that looks like this:
{'Aitkin': ['Carlton', 'Cass', 'Crow Wing', 'Itasca',
'Kanabec', 'Mille Lacs', 'Pine', 'St. Louis'], 'Anoka':
['Chisago', 'Hennepin', 'Isanti', 'Ramsey', 'Sherburne',
'Washington'], 'Becker': ['Clay', 'Clearwater', 'Hubbard',
'Mahnomen', 'Norman', 'Otter Tail', 'Wadena'], 'Beltrami':
['Cass', 'Clearwater', 'Hubbard', 'Itasca', 'Koochiching',
'Lake of the Woods', 'Marshall', 'Pennington', 'Roseau'],
'Benton': ['Mille Lacs', 'Morrison', 'Sherburne', 'Stearns'], 'Big
Stone': ['Lac qui Parle', 'Stevens', 'Swift', 'Traverse'], 'Blue
Earth': ['Brown', 'Faribault', 'Le Sueur', 'Martin',
'Nicollet', 'Waseca', 'Watonwan'], 'Brown': ['Blue Earth',
'Cottonwood', 'Nicollet', 'Redwood', 'Renville', 'Watonwan'],
'Carlton': ['Aitkin', 'Pine', 'St. Louis'], 'Carver': ['Hennepin',
'McLeod', 'Scott', 'Sibley', 'Wright'], 'Cass': ['Aitkin',
'Beltrami', 'Crow Wing', 'Hubbard', 'Itasca', 'Morrison',
'Todd', 'Wadena'], 'Chippewa': ['Kandiyohi', 'Lac qui Parle',
'Renville', 'Swift', 'Yellow Medicine'], 'Chisago': ['Anoka',
'Isanti', 'Kanabec', 'Pine', 'Washington'], 'Clay': ['Becker',
'Norman', 'Otter Tail', 'Wilkin'], 'Clearwater': ['Becker',
'Beltrami', 'Hubbard', 'Mahnomen', 'Pennington', 'Polk'],
'Cook': ['Lake'], 'Cottonwood': ['Brown', 'Jackson', 'Murray',
'Nobles', 'Redwood', 'Watonwan'], 'Crow Wing': ['Aitkin', 'Cass',
'Mille Lacs', 'Morrison'], 'Dakota': ['Goodhue', 'Hennepin',
'Ramsey', 'Rice', 'Scott', 'Washington'], 'Dodge': ['Goodhue',
'Mower', 'Olmsted', 'Rice', 'Steele'], 'Douglas': ['Grant', 'Otter
Tail', 'Pope', 'Stearns', 'Stevens', 'Todd'], 'Faribault': ['Blue
Earth', 'Freeborn', 'Martin', 'Waseca'], 'Fillmore': ['Houston',
'Mower', 'Olmsted', 'Winona'], 'Freeborn': ['Faribault', 'Mower',
'Steele', 'Waseca'], 'Goodhue': ['Dakota', 'Dodge', 'Olmsted',
'Rice', 'Wabasha'], 'Grant': ['Douglas', 'Otter Tail', 'Pope',
'Stevens', 'Traverse', 'Wilkin'], 'Hennepin': ['Anoka', 'Carver',
'Dakota', 'Ramsey', 'Scott', 'Sherburne', 'Wright'],
'Houston': ['Fillmore', 'Winona'], 'Hubbard': ['Becker', 'Beltrami',
'Cass', 'Clearwater', 'Wadena'], 'Isanti': ['Anoka', 'Chisago',
'Kanabec', 'Mille Lacs', 'Pine', 'Sherburne'], 'Itasca': ['Aitkin',
'Beltrami', 'Cass', 'Koochiching', 'St. Louis'], 'Jackson':
['Cottonwood', 'Martin', 'Nobles', 'Watonwan'], 'Kanabec': ['Aitkin',
'Chisago', 'Isanti', 'Mille Lacs', 'Pine'], 'Kandiyohi': ['Chippewa',
'Meeker', 'Pope', 'Renville', 'Stearns', 'Swift'], 'Kittson':
['Marshall', 'Roseau'], 'Koochiching': ['Beltrami', 'Itasca', 'Lake
of the Woods', 'St. Louis'], 'Lac qui Parle': ['Big Stone',
'Chippewa', 'Swift', 'Yellow Medicine'], 'Lake': ['Cook', 'St.
Louis'], 'Lake of the Woods': ['Beltrami', 'Koochiching', 'Roseau'],
'Le Sueur': ['Blue Earth', 'Nicollet', 'Rice', 'Scott', 'Sibley',
'Waseca'], 'Lincoln': ['Lyon', 'Pipestone', 'Yellow Medicine'],
'Lyon': ['Lincoln', 'Murray', 'Pipestone', 'Redwood', 'Yellow
Medicine'], 'Mahnomen': ['Becker', 'Clearwater', 'Norman', 'Polk'],
'Marshall': ['Beltrami', 'Kittson', 'Pennington', 'Polk', 'Roseau'],
'Martin': ['Blue Earth', 'Faribault', 'Jackson', 'Watonwan'],
'McLeod': ['Carver', 'Meeker', 'Renville', 'Sibley', 'Wright'],
'Meeker': ['Kandiyohi', 'McLeod', 'Renville', 'Stearns', 'Wright'],
'Mille Lacs': ['Aitkin', 'Benton', 'Crow Wing', 'Isanti',
'Kanabec', 'Morrison', 'Sherburne'], 'Morrison': ['Benton',
'Cass', 'Crow Wing', 'Mille Lacs', 'Stearns', 'Todd'], 'Mower':
['Dodge', 'Fillmore', 'Freeborn', 'Olmsted', 'Steele'], 'Murray':
['Cottonwood', 'Lyon', 'Nobles', 'Pipestone', 'Redwood', 'Rock'],
'Nicollet': ['Blue Earth', 'Brown', 'Le Sueur', 'Renville', 'Sibley'],
'Nobles': ['Cottonwood', 'Jackson', 'Murray', 'Rock'], 'Norman':
['Becker', 'Clay', 'Mahnomen', 'Polk'], 'Olmsted': ['Dodge',
'Fillmore', 'Goodhue', 'Mower', 'Wabasha', 'Winona'], 'Otter Tail':
['Becker', 'Clay', 'Douglas', 'Grant', 'Wadena', 'Wilkin'],
'Pennington': ['Beltrami', 'Clearwater', 'Marshall', 'Polk', 'Red
Lake'], 'Pine': ['Aitkin', 'Carlton', 'Chisago', 'Isanti',
'Kanabec'], 'Pipestone': ['Lincoln', 'Lyon', 'Murray', 'Rock'],
'Polk': ['Clearwater', 'Mahnomen', 'Marshall', 'Norman',
'Pennington', 'Red Lake'], 'Pope': ['Douglas', 'Grant',
'Kandiyohi', 'Stearns', 'Stevens', 'Swift'], 'Ramsey': ['Anoka',
'Dakota', 'Hennepin', 'Washington'], 'Red Lake': ['Pennington',
'Polk'], 'Redwood': ['Brown', 'Cottonwood', 'Lyon', 'Murray',
'Renville', 'Yellow Medicine'], 'Renville': ['Brown', 'Chippewa',
'Kandiyohi', 'McLeod', 'Meeker', 'Nicollet', 'Redwood',
'Sibley', 'Yellow Medicine'], 'Rice': ['Dakota', 'Dodge',
'Goodhue', 'Le Sueur', 'Scott', 'Steele', 'Waseca'], 'Rock':
['Murray', 'Nobles', 'Pipestone'], 'Roseau': ['Beltrami', 'Kittson',
'Lake of the Woods', 'Marshall'], 'Scott': ['Carver', 'Dakota',
'Hennepin', 'Le Sueur', 'Rice', 'Sibley'], 'Sherburne': ['Anoka',
'Benton', 'Hennepin', 'Isanti', 'Mille Lacs', 'Stearns',
'Wright'], 'Sibley': ['Carver', 'Le Sueur', 'McLeod', 'Nicollet',
'Renville', 'Scott'], 'St. Louis': ['Aitkin', 'Carlton', 'Itasca',
'Koochiching', 'Lake'], 'Stearns': ['Benton', 'Douglas',
'Kandiyohi', 'Meeker', 'Morrison', 'Pope', 'Sherburne',
'Todd', 'Wright'], 'Steele': ['Dodge', 'Freeborn', 'Mower', 'Rice',
'Waseca'], 'Stevens': ['Big Stone', 'Douglas', 'Grant', 'Pope',
'Swift', 'Traverse'], 'Swift': ['Big Stone', 'Chippewa',
'Kandiyohi', 'Lac qui Parle', 'Pope', 'Stevens'], 'Todd':
['Cass', 'Douglas', 'Morrison', 'Otter Tail', 'Stearns', 'Wadena'],
'Traverse': ['Big Stone', 'Grant', 'Stevens', 'Wilkin'], 'Wabasha':
['Goodhue', 'Olmsted', 'Winona'], 'Wadena': ['Becker', 'Cass',
'Hubbard', 'Otter Tail', 'Todd'], 'Waseca': ['Blue Earth',
'Faribault', 'Freeborn', 'Le Sueur', 'Rice', 'Steele'],
'Washington': ['Anoka', 'Chisago', 'Dakota', 'Ramsey'], 'Watonwan':
['Blue Earth', 'Brown', 'Cottonwood', 'Jackson', 'Martin'], 'Wilkin':
['Clay', 'Grant', 'Otter Tail', 'Traverse'], 'Winona': ['Fillmore',
'Houston', 'Olmsted', 'Wabasha'], 'Wright': ['Carver', 'Hennepin',
'McLeod', 'Meeker', 'Sherburne', 'Stearns'], 'Yellow Medicine':
['Chippewa', 'Lac qui Parle', 'Lincoln', 'Lyon', 'Redwood',
'Renville']}
The keys in the dictionary represent the nodes, while the values(lists) represent nodes of neighbors of the key. This is an undirected unweighted graph.
Is there some function to implement this in networkx or some network analysis or graph-related libraries?
You can use the nx.Graph constructor -- it will add additional nodes if they don't appear as keys in the original dictionary (data represents the dictionary in the original question):
import networkx as nx
graph = nx.Graph(data)

Nested Python Object to CSV

I looked up "nested dict" and "nested list" but either method work.
I have a python object with the following structure:
[{
'id': 'productID1', 'name': 'productname A',
'option': {
'size': {
'type': 'list',
'name': 'size',
'choices': [
{'value': 'M'},
]}},
'variant': [{
'id': 'variantID1',
'choices':
{'size': 'M'},
'attributes':
{'currency': 'USD', 'price': 1}}]
}]
what i need to output is a csv file in the following, flattened structure:
id, productname, variantid, size, currency, price
productID1, productname A, variantID1, M, USD, 1
productID1, productname A, variantID2, L, USD, 2
productID2, productname A, variantID3, XL, USD, 3
i tried this solution: Python: Writing Nested Dictionary to CSV
or this one: From Nested Dictionary to CSV File
i got rid of the [] around and within the data and e.g. i used this code snippet from 2 and adapted it to my needs. IRL i can't get rid of the [] because that's simple the format i get when calling the API.
with open('productdata.csv', 'w', newline='', encoding='utf-8') as output:
writer = csv.writer(output, delimiter=';', quotechar = '"', quoting=csv.QUOTE_NONNUMERIC)
for key in sorted(data):
value = data[key]
if len(value) > 0:
writer.writerow([key, value])
else:
for i in value:
writer.writerow([key, i, value])
but the output is like this:
"id";"productID1"
"name";"productname A"
"option";"{'size': {'type': 'list', 'name': 'size', 'choices': {'value': 'M'}}}"
"variant";"{'id': 'variantID1', 'choices': {'size': 'M'}, 'attributes': {'currency': 'USD', 'price': 1}}"
anyone can help me out, please?
thanks in advance
list indices must be integers not strings
The following presents a visual example of a python list:
0 carrot.
1 broccoli.
2 asparagus.
3 cauliflower.
4 corn.
5 cucumber.
6 eggplant.
7 bell pepper
0, 1, 2 are all "indices".
"carrot", "broccoli", etc... are all said to be "values"
Essentially, a python list is a machine which has integer inputs and arbitrary outputs.
Think of a python list as a black-box:
A number, such as 5, goes into the box.
you turn a crank handle attached to the box.
Maybe the string "cucumber" comes out of the box
You got an error: TypeError: list indices must be integers or slices, not str
There are various solutions.
Convert Strings into Integers
Convert the string into an integer.
listy_the_list = ["carrot", "broccoli", "asparagus", "cauliflower"]
string_index = "2"
integer_index = int(string_index)
element = listy_the_list[integer_index]
so yeah.... that works as long as your string-indicies look like numbers (e.g. "456" or "7")
The integer class constructor, int(), is not very smart.
For example, x = int("3 ") will produce an error.
You can try x = int(strying.strip()) to get rid of leading and trailing white-space characters.
Use a Container which Allows Keys to be Strings
Long ago, before before electronic computers existed, there were various types of containers in the world:
cookie jars
muffin tins
carboard boxes
glass jars
steel cans.
back-packs
duffel bags
closets/wardrobes
brief-cases
In computer programming there are also various types of "containers"
You do not have to use a list as your container, if you do not want to.
There are containers where the keys (AKA indices) are allowed to be strings, instead of integers.
In python, the standard container which like a list, but where the keys/indices can be strings, is a dictionary
thisdict = {
"make": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["brand"] == "Ford"
If you want to index into a container using strings, instead of integers, then use a dict, instead of a list
The following is an example of a python dict which has state names as input and state abreviations as output:
us_state_abbrev = {
'Alabama': 'AL',
'Alaska': 'AK',
'American Samoa': 'AS',
'Arizona': 'AZ',
'Arkansas': 'AR',
'California': 'CA',
'Colorado': 'CO',
'Connecticut': 'CT',
'Delaware': 'DE',
'District of Columbia': 'DC',
'Florida': 'FL',
'Georgia': 'GA',
'Guam': 'GU',
'Hawaii': 'HI',
'Idaho': 'ID',
'Illinois': 'IL',
'Indiana': 'IN',
'Iowa': 'IA',
'Kansas': 'KS',
'Kentucky': 'KY',
'Louisiana': 'LA',
'Maine': 'ME',
'Maryland': 'MD',
'Massachusetts': 'MA',
'Michigan': 'MI',
'Minnesota': 'MN',
'Mississippi': 'MS',
'Missouri': 'MO',
'Montana': 'MT',
'Nebraska': 'NE',
'Nevada': 'NV',
'New Hampshire': 'NH',
'New Jersey': 'NJ',
'New Mexico': 'NM',
'New York': 'NY',
'North Carolina': 'NC',
'North Dakota': 'ND',
'Northern Mariana Islands':'MP',
'Ohio': 'OH',
'Oklahoma': 'OK',
'Oregon': 'OR',
'Pennsylvania': 'PA',
'Puerto Rico': 'PR',
'Rhode Island': 'RI',
'South Carolina': 'SC',
'South Dakota': 'SD',
'Tennessee': 'TN',
'Texas': 'TX',
'Utah': 'UT',
'Vermont': 'VT',
'Virgin Islands': 'VI',
'Virginia': 'VA',
'Washington': 'WA',
'West Virginia': 'WV',
'Wisconsin': 'WI',
'Wyoming': 'WY'
}
i could actually iterate this list and create my own sublist, e.g. e list of variants
data = [{
'id': 'productID1', 'name': 'productname A',
'option': {
'size': {
'type': 'list',
'name': 'size',
'choices': [
{'value': 'M'},
]}},
'variant': [{
'id': 'variantID1',
'choices':
{'size': 'M'},
'attributes':
{'currency': 'USD', 'price': 1}}]
},
{'id': 'productID2', 'name': 'productname B',
'option': {
'size': {
'type': 'list',
'name': 'size',
'choices': [
{'value': 'XL', 'salue':'XXL'},
]}},
'variant': [{
'id': 'variantID2',
'choices':
{'size': 'XL', 'size2':'XXL'},
'attributes':
{'currency': 'USD', 'price': 2}}]
}
]
new_list = {}
for item in data:
new_list.update(id=item['id'])
new_list.update (name=item['name'])
for variant in item['variant']:
new_list.update (varid=variant['id'])
for vchoice in variant['choices']:
new_list.update (vsize=variant['choices'][vchoice])
for attribute in variant['attributes']:
new_list.update (vprice=variant['attributes'][attribute])
for option in item['option']['size']['choices']:
new_list.update (osize=option['value'])
print (new_list)
but the output is always the last item of the iteration, because i always overwrite new_list with update().
{'id': 'productID2', 'name': 'productname B', 'varid': 'variantID2', 'vsize': 'XXL', 'vprice': 2, 'osize': 'XL'}
here's the final solution which worked for me:
data = [{
'id': 'productID1', 'name': 'productname A',
'variant': [{
'id': 'variantID1',
'choices':
{'size': 'M'},
'attributes':
{'currency': 'USD', 'price': 1}},
{'id':'variantID2',
'choices':
{'size': 'L'},
'attributes':
{'currency':'USD', 'price':2}}
]
},
{
'id': 'productID2', 'name': 'productname B',
'variant': [{
'id': 'variantID3',
'choices':
{'size': 'XL'},
'attributes':
{'currency': 'USD', 'price': 3}},
{'id':'variantID4',
'choices':
{'size': 'XXL'},
'attributes':
{'currency':'USD', 'price':4}}
]
}
]
for item in data:
for variant in item['variant']:
dic = {}
dic.update (ProductID=item['id'])
dic.update (Name=item['name'].title())
dic.update (ID=variant['id'])
dic.update (size=variant['choices']['size'])
dic.update (Price=variant['attributes']['price'])
products.append(dic)
keys = products[0].keys()
with open('productdata.csv', 'w', newline='', encoding='utf-8') as output_file:
dict_writer = csv.DictWriter(output_file, keys,delimiter=';', quotechar = '"', quoting=csv.QUOTE_NONNUMERIC)
dict_writer.writeheader()
dict_writer.writerows(products)
with the following output:
"ProductID";"Name";"ID";"size";"Price"
"productID1";"Productname A";"variantID1";"M";1
"productID1";"Productname A";"variantID2";"L";2
"productID2";"Productname B";"variantID3";"XL";3
"productID2";"Productname B";"variantID4";"XXL";4
which is exactly what i wanted.

Dynamically naming saved dataframes in loop

I'm attempting to use the GTab package to query Google Search trends data for every state in the US, but am having some trouble getting my loop to work.
For one state it's easy enough to do this, and new_query produces a dataframe.
t = gtab.GTAB()
t.set_options(pytrends_config={"geo": "US-NY", "timeframe": "2020-09-01 2020-10-01"})
query = t.new_query("weather")
To loop through I'm trying to use a dict to assign geo dynamically. However, I can't figure out how to do the same for the df name (query).
state_abbrevs = {
'Alabama': 'AL',
'Alaska': 'AK',
'Arizona': 'AZ',
'Arkansas': 'AR',
'California': 'CA',
'Colorado': 'CO',
'Connecticut': 'CT',
'Delaware': 'DE',
'District of Columbia': 'DC',
'Florida': 'FL',
'Georgia': 'GA',
'Guam': 'GU',
'Hawaii': 'HI',
'Idaho': 'ID',
'Illinois': 'IL',
'Indiana': 'IN',
'Iowa': 'IA',
'Kansas': 'KS',
'Kentucky': 'KY',
'Louisiana': 'LA',
'Maine': 'ME',
'Maryland': 'MD',
'Massachusetts': 'MA',
'Michigan': 'MI',
'Minnesota': 'MN',
'Mississippi': 'MS',
'Missouri': 'MO',
'Montana': 'MT',
'Nebraska': 'NE',
'Nevada': 'NV',
'New Hampshire': 'NH',
'New Jersey': 'NJ',
'New Mexico': 'NM',
'New York': 'NY',
'North Carolina': 'NC',
'North Dakota': 'ND',
'Northern Mariana Islands':'MP',
'Ohio': 'OH',
'Oklahoma': 'OK',
'Oregon': 'OR',
'Pennsylvania': 'PA',
'Puerto Rico': 'PR',
'Rhode Island': 'RI',
'South Carolina': 'SC',
'South Dakota': 'SD',
'Tennessee': 'TN',
'Texas': 'TX',
'Utah': 'UT',
'Vermont': 'VT',
'Virgin Islands': 'VI',
'Virginia': 'VA',
'Washington': 'WA',
'Washington DC' : 'DC',
'West Virginia': 'WV',
'Wisconsin': 'WI',
'Wyoming': 'WY'
}
for v in state_abbrevs.values():
t = gtab.GTAB()
t.set_options(pytrends_config={"geo": f"US-{v}", "timeframe": "2020-09-01 2020-10-01"})
query = t.new_query("weather")
I've tried using an f string but that produces SyntaxError: can't assign to literal.
I used two answers from here. I think your best option is just storing the DataFrames in a dictionary but this should work to create your query_* variables.
query_dict = {}
for n, v in enumerate(state_abbrevs.values()):
t = gtab.GTAB()
t.set_options(pytrends_config={"geo": f"US-{v}", "timeframe": "2020-09-01 2020-10-01"})
query = t.new_query("weather")
key = "query_" + str(n)
query_dict[key] = query
for k in query_dict.keys():
exec("%s = query_dict['%s']" % (k,k))

Convert the nested json into a dictionary format with no nested objects

I have the input data in below format:
data = [[u'Richard', u'48', [u'Josh', u'Beth'], {u'city': u'Seattle', u'Disability': u'no', u'enterprenuer': u'yes'}], [u'Bryan', u'32',[], {u'city': u'NY', u'enterprenuer': u'no', u'wfh': u'yes', u'disability': u'no', u'Visa': u'no'}]]
Which later on doing json.dumps becomes:
[["Richard", "48", ["Josh", "Beth"], {"city": "Seattle", "enterprenuer": "yes", "Disability": "no"}], ["Bryan", "32", [], {"Visa": "no", "city": "NY", "wfh": "yes", "enterprenuer": "no", "disability": "no"}]]
Also, I have another list which holds the keys for the dict:
key_list = ["Name", "Age", "Children", "details"]
I tried the below code:
list_of_dicts = []
for d in data:
dict = {}
for i in range(0, len(key_list)-1):
dict[key_list[i]] = d[i]
list_of_dicts.append(dict)
With this I was able to get new_dict:
[{'Age': u'48', 'Name': u'Richard', 'Children': [u'Josh', u'Beth']}, {'Age': u'32', 'Name': u'Bryan', 'Children': []}]
But I am not able to get the nested dict from data into the new_dict without the need to run code on it again. I don't want to run operation multiple times.
Also, I was thinking if there's any better way to remove the nested list as well but after multiple hit and trial I got side tracked and messed up my code.
This is the expected output:
[{"Name":"Richard","Age":"48","Children":"Josh,Beth","city":"Seattle","enterprenuer":"yes","Disability":"no"},{"Name":"Bryan","Age":"32","Children":"","Visa":"no","city":"NY","wfh":"yes","enterprenuer":"no","disability":"no"}]
you can try:
1) in python3:
from pprint import pprint
data = [["Richard", "48", ["Josh", "Beth"], {"city": "Seattle", "enterprenuer": "yes", "Disability": "no"}], ["Bryan", "32", [], {"Visa": "no", "city": "NY", "wfh": "yes", "enterprenuer": "no", "disability": "no"}]]
key_list = ["Name", "Age", "Children", "details"]
pprint([dict(zip(key_list[:2], e[:2]), **{key_list[2]: ','.join(e[2])}, **e[3]) for e in data])
output:
[{'Name': 'Richard',
'Age': '48',
'Children': 'Josh,Beth',
'city': 'Seattle',
'Disability': 'no',
'enterprenuer': 'yes'},
{'Name': 'Bryan',
'Age': '32',
'Children': '',
'city': 'NY',
'enterprenuer': 'no',
'wfh': 'yes',
'disability': 'no',
'Visa': 'no'}]
2) in python2:
pprint([dict(zip(key_list[:2], e[:2]), **dict([(key_list[2], ','.join(e[2]))], **e[3])) for e in data])
output:
[{'Age': '48',
'Children': 'Josh,Beth',
'Disability': 'no',
'Name': 'Richard',
'city': 'Seattle',
'enterprenuer': 'yes'},
{'Age': '32',
'Children': '',
'Name': 'Bryan',
'Visa': 'no',
'city': 'NY',
'disability': 'no',
'enterprenuer': 'no',
'wfh': 'yes'}]
data = [["Richard", "48", ["Josh", "Beth"], {"city": "Seattle", "enterprenuer": "yes", "Disability": "no"}], ["Bryan", "32", [], {"Visa": "no", "city": "NY", "wfh": "yes", "enterprenuer": "no", "disability": "no"}]]
key_list = ["Name", "Age", "Children", "details"]
out = []
for item in data:
d = {}
out.append(d)
for value, keyname in zip(item, key_list):
if isinstance(value, dict):
d.update(**value)
elif isinstance(value, list):
d[keyname] = ','.join(value)
else:
d[keyname] = value
from pprint import pprint
pprint(out)
Prints:
[{'Age': '48',
'Children': 'Josh,Beth',
'Disability': 'no',
'Name': 'Richard',
'city': 'Seattle',
'enterprenuer': 'yes'},
{'Age': '32',
'Children': '',
'Name': 'Bryan',
'Visa': 'no',
'city': 'NY',
'disability': 'no',
'enterprenuer': 'no',
'wfh': 'yes'}]
You can use simple unpacking:
data = [[u'Richard', u'48', [u'Josh', u'Beth'], {u'city': u'Seattle', u'Disability': u'no', u'enterprenuer': u'yes'}], [u'Bryan', u'32',[], {u'city': u'NY', u'enterprenuer': u'no', u'wfh': u'yes', u'disability': u'no', u'Visa': u'no'}]]
key_list = ["Name", "Age", "Children", "details"]
r = [{**dict(zip(key_list[:-1], a[:-1]+[','.join(a[-1])])), **b} for *a, b in data]
Output:
[{'Name': 'Richard', 'Age': '48', 'Children': 'Josh,Beth', 'city': 'Seattle', 'Disability': 'no', 'enterprenuer': 'yes'}, {'Name': 'Bryan', 'Age': '32', 'Children': '', 'city': 'NY', 'enterprenuer': 'no', 'wfh': 'yes', 'disability': 'no', 'Visa': 'no'}]
Edit: Python2.7 solution:
data = [[u'Richard', u'48', [u'Josh', u'Beth'], {u'city': u'Seattle', u'Disability': u'no', u'enterprenuer': u'yes'}], [u'Bryan', u'32',[], {u'city': u'NY', u'enterprenuer': u'no', u'wfh': u'yes', u'disability': u'no', u'Visa': u'no'}]]
key_list = ["Name", "Age", "Children", "details"]
r = [dict(zip(key_list[:-1], i[:2]+[','.join(i[2])])+i[-1].items()) for i in data]
Output:
[{u'city': u'Seattle', 'Name': u'Richard', 'Age': u'48', u'enterprenuer': u'yes', u'Disability': u'no', 'Children': u'Josh,Beth'}, {u'city': u'NY', u'wfh': u'yes', 'Name': u'Bryan', 'Age': u'32', u'enterprenuer': u'no', u'disability': u'no', u'Visa': u'no', 'Children': ''}]

Python 3 : Print a list that's inside a list

I cannot seem to print a list that's within a list. I know that it goes wrong when I repeat the for loop for key and values. I was wondering if there is a way to make so that it prints each troop name for each platoon.
import random, math
first_name = ['John', 'Clay', 'Gordy', 'Erv', 'Sebastian', 'Tilly', 'Jesse', 'Alban', 'Oliver', 'Samuel', 'Joseph', 'Gregory', 'Alair', 'Gilbert', 'Nigel', 'Gibson', 'Oliver', 'Ralph', 'Rufus', 'Garson', 'Ferrol', 'Miles', 'Chilton', 'Charles', 'Gordon', 'Edward', 'Gerald', 'Shel', 'Dean', 'Noah', 'Herbert', 'Humphrey', 'Hanley', 'Ruben', 'Gibson', 'Jonathan', 'Fisk', 'Harold', 'Cristian', 'Andy', 'Kyne', 'Garson', 'Jackson', 'Maitland', 'George', 'Ford', 'Raleigh', 'Fox', 'Forbes', 'Yeardleigh', 'Gordon', 'Francis', 'Jett', 'Fairfax', 'Ford', 'Haines', 'Benjamin', 'Samuel', 'Alban', 'Chip', 'Eric', 'Alban', 'Charles', 'Sherman', 'Harrison', 'Malcolm', 'Chilton', 'Eliah', 'Junior', 'Mark', 'Bond', 'Chick', 'Emmanuel', 'Raleigh', 'Brigham', 'Archibald', 'Gates', 'Filbert', 'Barnabas', 'Geoffrey', 'Terence', 'Stacy', 'Forbes', 'Gomer', 'Fairly', 'Archer', 'Oscar', 'William', 'Ernes', 'Chill', 'Gregory', 'Weylin', 'Holt', 'Clayland', 'Gram', 'Forbes', 'Set', 'Hartwell', 'Luke', 'Garson']
last_name = ['Mitchell', 'Martin', 'Anderson', 'Patel', 'Young', 'Jackson', 'Ward', 'Jackson', 'Patel', 'Walker', 'Lee', 'Patel', 'Johnson', 'Thomas', 'Morris', 'Watson', 'Martin', 'Roberts', 'Jones', 'Lewis', 'Morgan', 'Wood', 'Lee', 'White', 'James', 'Scott', 'Young', 'Clarke', 'Edwards', 'Smith', 'Jackson', 'Turner', 'Ward', 'Hall', 'Anderson', 'Walker', 'Scott', 'Mitchell', 'Williams', 'Young', 'Allen', 'Huges', 'Phillips', 'Robinson', 'Evans', 'Thomas', 'Taylor', 'Robinson', 'Harris', 'Ward', 'Johnson', 'Anderson', 'Scott', 'Martin', 'Allen', 'Clark', 'Jones', 'Wilson', 'Phillips', 'Lewis', 'Jones', 'Anderson', 'Wright', 'Clark', 'White', 'Lewis', 'Patel', 'Wilson', 'Wilson', 'Taylor', 'Williams', 'Turner', 'Smith', 'Davies', 'Harrison', 'Thompson', 'Anderson', 'Harris', 'Brown', 'Lewis', 'Phillips', 'Watson', 'Harrison', 'Harris', 'Wilson', 'Davies', 'Brown', 'Huges', 'Parker', 'King', 'Wright', 'Anderson', 'Anderson', 'Phillips', 'Harrison', 'Walker', 'Wood', 'Young', 'Clark', 'Jones']
troops = []
temp_platoon = []
platoons = []
platoon_names = ['Alpha', 'Beta', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel', 'India', 'Juliet', 'Kilo', 'Lima', 'Mike', 'November', 'Oscar', 'Papa', 'Quebec', 'Romeo', 'Sierra', 'Tango', 'Uniform', 'Victor', 'Whiskey', 'X-Ray', 'Yankee', 'Zulu']
a = 0
while a < 90:
s_name = random.choice(first_name) + " " + random.choice(last_name)
s_number = 100
troops.append((s_name, s_number))
a = a + 1
platoon_number = 1
troop_number = 0
for key in troops :
troop_number = troop_number + 1
a = troops.pop(0)
temp_platoon.append((a))
if troop_number == 30:
# Platoon n is the name of platoon
# Platoon is the actual platoon
platoon_number = platoon_number + 1
platoon = platoon_names.pop(0)
platoon_n = platoon
platoon = []
for k, v in temp_platoon:
platoon.append((k, v))
print("Added {} to platoon {}. He has {} health".format(k, platoon_n, v))
platoons.append((platoon_n))
troop_number = 0
def read ():
print("Reading")
for key in platoons:
print(key)
for w in key:
print(w)
print(platoons)
read()
Also note im teaching myself python 3. I have only just started touching on classes.
I may have it wrong, but I think you might want it to look something like this:
import random
first_name = ['John', 'Clay', 'Gordy', 'Erv']
last_name = ['Mitchell', 'Martin', 'Anderson', 'Patel']
platoon_names = ['Alpha', 'Beta', 'Charlie', 'Delta']
platoons = dict()
using a list comprehension we can completely replace the while loop. every soldier is a tuple (name, health)... I presume you wanted this, but if not just remove the 100).
troops = [(random.choice(first_name) + " " + random.choice(last_name), 100) for r in range(90)]
the following helper function is lifter from here
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i+n]
we now populate the platoons dictionary (the key is the platoon name, while the value is a list containing the soldiers).
for number, platoon in enumerate(list(chunks(troops,3))):
platoons[platoon_names[number]] = platoon
use iteritems to iterate over a dictionary.
def read ():
print("Reading")
for platoon, troops in platoons.iteritems():
print("--------Platoon name--------")
print(platoon)
print("--------troops--------")
for (name, health) in troops:
print(name)
and this is what we get.
>>> read()
Reading
--------Platoon name--------
Alpha
--------troops--------
John Anderson
Clay Mitchell
Erv Martin
--------Platoon name--------
Beta
--------troops--------
Clay Anderson
Clay Anderson
Clay Patel
--------Platoon name--------
Delta
--------troops--------
John Anderson
Gordy Martin
John Anderson
--------Platoon name--------
Charlie
--------troops--------
Clay Mitchell
Clay Patel
Clay Patel
>>>

Categories

Resources