How to convert between csv.reader and csv.DictReader objects? [duplicate] - python

This question already has answers here:
Is it possible to keep the column order using csv.DictReader?
(6 answers)
Closed 9 years ago.
I have the need to deal using cvs.reader objects because of the order of the columnts must be preserved.
But, I would like also to have the csv.DictReader object for some other reasons.
Now, is there a simple posibility to convert them between each other without having re-reading the file?

I'd recommend you to use tablib instead.
ds = tablib.Dataset()
ds.csv = open(csvfile).read()
Then you can iter over ds as a sequence of rows, and ds.dict gives you a list of OrderedDict instances for each row. It's much easier if you also need to get it to other formats, or edit and save again as csv.

Related

join two JSON objects in Python on common data point [duplicate]

This question already has answers here:
Why does creating a list of tuples using list comprehension requires parentheses?
(2 answers)
Why do tuples in a list comprehension need parentheses? [duplicate]
(3 answers)
Closed 8 months ago.
I keep getting a syntax error for this and Google is no help for my specific issue.
I'm trying to merge two data sets into a single dictionary. One data set comes from https://universalis.app/api/v2/marketable and looks to be an array. The other comes from https://raw.githubusercontent.com/ffxiv-teamcraft/ffxiv-teamcraft/master/apps/client/src/assets/data/items.json and appears to be just an object of objects. Example below with what I've tried.
Code:
import requests
import json
url = "https://universalis.app/api/v2/marketable"
response = json.loads(requests.get(url).text)
marketableItems = [
item
for item in response
]
url = "https://raw.githubusercontent.com/ffxiv-teamcraft/ffxiv-teamcraft/master/apps/client/src/assets/data/items.json"
allItemsResponse = json.loads(requests.get(url).text)
itemDictionary = [
Item, allItemsResponse[str(Item)]["en"]
for Item in marketableItems
]
this produces:
Item, allItemsResponse[str(Item)]["en"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: did you forget parentheses around the comprehension target?
I've googled a fair bit for this exact Syntax Error, but I'm not really able to find any sort of guide on how to join two objects like this. I'm able to get allItemsResponse[str(Item)]["en"] to return data, I just want it paired with the original data from the first URL.

Shuffle a copy of a list [duplicate]

This question already has an answer here:
How to properly copy a list in python
(1 answer)
Closed 2 years ago.
It seems that the copy of a list is shuffled too?
So if I do for 2 lists:
data = examples
np.random.shuffle(examples)
then the list data is also shuffled? Why?
Python doesn't create copies of objects when it thinks they're not needed.
In your case you can use built-in copy module:
import copy
data = copy.deepcopy(examples)
np.random.shuffle(examples)
You have to do examples = np.copy(data) because otherwise the two variables will reference the same position in memory meaning they both change.

Name a Dictionary after a known string [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
I've seen this question a few times before, and invariably I see that people always respond by saying that this is a bad idea. What is a good way to approach this issue?
I have a code that reads in data from several csv files in a for loop, i.e for each loop iteration a different csv is read. At the end of my for loop, I make a dictionary with some data in it. All I would like to do is name this dictionary after the original csv file's name.
At the moment I have:
sessionname=str(inputList).replace(".csv",'').replace("'",'').replace("[",'').replace("]",'')
session_dict={}
What I would like is for session_dict to be named using the string in sessionname.
Therefore at the end of my for loop, I would have a number of dictionaries each with the name of its orginal csv file.
Thank you!
You can't create a variable named after a filepath, because of the slashes and the spaces, etc.
One thing you can do is to name each of your dictionaries with a number (or a unique code), and in run-time create a reference dictionary like:
ref_dict = {r'csv_file_0_path' :0, r'csv_file_1_path': 1, r'csv_file_2_path': 2}
and so on.

Possible to convert string to a named variable or alternative? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
How can I create multiple variables from a list of strings? [duplicate]
(2 answers)
generating variable names on fly in python [duplicate]
(6 answers)
Closed 3 years ago.
I have a ticker and I want to check a specific list of tickers to see if the ticker is found. If it is found, it will replace it.
The new tickers come from another data source and therefore do not know which specific list of tickers to check. In order to find that list, I can pass the lists name as a string but upon iterating the code (naturally) recognizes this as string as opposed to a list to iterate.
Is there a way to have the code/function recognize that the string is actually a specific list to be checked? In reading other questions, I know this may not be possible...in that case what is an alternative?
list_1=['A','B']
list_2=['C','D']
old_ticker='A'
new_ticker='E'
assigned_list='list_1'
def replace_ticker(old_ticker,new_ticker,list):
for ticker in list:
if new_ticker in list:
return
else:
list.append(new_ticker)
list.remove(old_ticker)
replace_ticker(old_ticker,new_ticker,assigned_list)
You key the needed lists by name in a dictionary:
ticker_directory = {
"list_1": list_1,
"list_2": list_2
}
Now you can accept the name and get the desired list as ticker_directory[assigned_list].
list_1=['A','B']
list_2=['C','D']
lists = {
'list_1':list_1,
'list_2':list_2
}
old_ticker='A'
new_ticker='E'
assigned_list='list_1'
def replace_ticker(old_ticker,new_ticker,list_name):
if old_ticker not in lists[list_name]:
return
else:
lists[list_name].append(new_ticker)
lists[list_name].remove(old_ticker)
replace_ticker(old_ticker,new_ticker,assigned_list)
print(lists[assigned_list])
This is the complete program from what i perceived.
#prune already answered this, I have just given the whole solution
There are at least two possibilities:
1 As noted in comments kind of overkill but possible:
Use eval() to evaluate string as python expressions more in the link:
https://thepythonguru.com/python-builtin-functions/eval/
For example:
list_name = 'list_1'
eval('{}.append(new_ticker)'.format(list_name))
2 Second
Using locals() a dictionary of locally scoped variables similiar to the other answers but without the need of creating the dict by hand which also requires the knowledge of all variables names.
list_name = 'list_1'
locals()[list_name].append(new_ticker)

Why use "dict()" at all in Python? [duplicate]

This question already has answers here:
Is there a difference between using a dict literal and a dict constructor?
(12 answers)
Closed 7 years ago.
Consider these two statements, which serve the same purpose:
tel = {'sape': 4139, 'jack': 4098}
and
tel = dict([('sape', 4139), ('jack', 4098)])
Why use "dict()" at all?
I am sure there is a reason, i just want to know it.
The reason for the existence of dict(...) is that all classes need to have a constructor. Furthermore, it may be helpful if the constructor is able to take in data in a different format.
In your example use case, there is no benefit in using dict, because you can control the format the data is in. But consider if you have the data already as pairs in a list, the dict constructor may be useful. This can happen e.g. when reading lines from a file.
map(dict,[[(1,2)]])
[{1: 2}]
map({},[[(1,2)]])
TypeError: 'dict' object is not callable

Categories