i have a problem with saving a complex dictionary into an excel file.
This is my code so far:
attention_relevance = model.get_attention(test)
attentionres = []
for key in attention_relevance.keys():
attentionres.append(attention_relevance[key])
dfattention = pd.DataFrame(attentionres)
dfattention.to_excel(r'/savepath/attention.xlsx', index = False)
The dictionary i would like to save is called "attention_relevance".
The code is running without any error messages, but in the excel-file some of the values are not written, instead they are replaced by "...".
Like this:
[[[0.02923768 0.02157122 0.02464608 ... 0.06667057 0.03331407 0.0075733 ].
How can I fix this? I need all the values in there.
Anyone who can help?
Thank you very much!
Hinnerk8
you seem to be saving a list in a list, not a dictionary or just 1 list.
or you can try
np.squeeze(attentionres)
and then save the file it would also work
Related
I'm fairly new to Python and Selenium.
I'm trying to gather elements from a webpage using Python and Selenium in VS Code.
I've already done similar things in other webpages so I can confirm the setups and the drivers all work fine.
Here is the code which I'll try to explain line by line.
'// Creating an empty Array of Names'
Names = []
'// Finding and Saving the Table im interested in'
Table = driver.find_element_by_id("pokedex")
'// Finding and Saving in a list all the elements with a particular class name'
NameCells = Table.find_elements_by_class_name("cell-name")
'// Looping through the List'
for NameCell in NameCells:
'// If it finds a child element with a particular class...'
if NameCell.find_elements(By.CLASS_NAME, "text-muted"):
'// ... append it in the array once transformed into text'
Names.append(NameCell.find_element(By.CLASS_NAME, "text-muted").text)
'// ... else...'
else:
'// ... append an element with another class into the array once transformed into text.'
Names.append(NameCell.find_element(By.CLASS_NAME, "ent-name").text)
'// .. and print the array.'
print(Names)
The problem is that while I can use functions like "find_element" in the second and third line of code... I can't use it in the for loop, in the fifth line of code.
VS Code doesn't even show me the expected functions after digiting the ".".
I tried to complete it myself hoping it worked but of course it didn't.
Why does it happen?
Why can't I use WebElements functions at times?
I'm noticing it's happening mainly on Lists of objects rather than single objects.
I have a problem with the Function Module "GUI_DOWNLOAD" because of the date converting.
I want to get the date like I have it in my internal table but CSV (Excel) keeps converting it everytime.
The internal table contains the line like this: 12345678;GroupDate;2021-12-31;
The Output in the .csv-File should be "2021-12-31" but it keeps converting to "31.12.2021".
I also tried to put an ' (apostroph) before the date but the output will be '2021-12-31
Does anybode have an Idea ?
lv_conv = '2021-12-31'.
CONCATENATE TEXT-001 LV_CONV INTO LV_CONV.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = IV_PATH
TABLES
DATA_TAB = LT_FILE.
LT_FILE is a string table.
Thanks for the help.
Like Suncatcher and Sandra said the file is right but it is only the settings from excel which convert the date.
If the Output file won´t be needed for other purposes than showing the code could be something like this
CONCATENATE '=("' LV_CONV '")' INTO LV_CONV.
The csv-Output would be a date like this 1960-01-01 but in the cell the value would look like =("1960-01-01").
I have an excel table with a column called 'interface', and what I want to do is a code that pull each interface value : Port-channel47, Port-channel46,etc... and put it in my .txt template replacing the {interface} part that i have in my txt template.
the value I want to change in the .txt template is "{interface}"
I tried this code:
but I get lost when i want to pull the data.
Anyone can help me? thank you so much in advance
Use string template:
from string import Template
with open('template.txt') as fp:
template = Template(fp.read())
for interface, group in df.groupby('Interface'):
file_name = interface.replace('/', '_') + '_output.txt'
with open(file_name, 'w') as fp:
content = template.substitute(interface=interface)
fp.write(content)
When you iterate over a DataFrameGroupBy object, it returns a 2-tuple containing the key of the group, and all rows matching that key.
A second thing to worry about is that / is not a valid path under most OSes I know so you must replace it with some other character (I chose a _).
I am importing some data from the excel and the code looks like this:
Code:
%Import Data
filename = 'Stocks.xlsx';
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(filename,delimiterIn,headerlinesIn);
The excel file looks like this:
When I read in the data, A looks like this:
I thought when the headerlineIn = 1, the first line should not read. Why is it that it is being read? How to avoid this?
Need some guidance..
How I thought your code is alright.
With your example file and your code I get a struct A.
A = importdata('Stocks.xlsx',' ',1);
In A.data.Sheet1 is all the data correctly read:
And in A.textdata.Sheet1 the appears what you posted you get.
So the problem must be something I can't reproduce.
Alternatively you could try if xlsread works for you.
B = xlsread('Stocks.xlsx',1)
I get the same result as before.
I finally get your problem, you're not concerned about the data, you really want to skip the first line of the header in the means of textdata.
Well headerlinesIn just signalizes importdata when your data starts, respectively when it should start to read actual data. Everything else, which is then declared not to be data, is put into A.textdata.Sheet1, also the first line. So the code works as intended.
If you want to get rid of the first line of your header, you could apply the following line:
N = 2; %// number of columns before data starts
A.textdata.Sheet1 = {A.textdata.Sheet1{headerlinesIn+1:end,1:N}};
This is really simple, and for some reason I am having a ton of difficulty doing this. Suppose I have an excel file: "Data.xls" I can do:
a = xlsread('Data.xls','Sheet1','A1:B10');
And i get the data I want. However, I want to write a function to do this:
`function [ data ] = ReadData( fileID,Sheet,Lines )
data = xlsread('fileID','Sheet','Lines');
end
And when I run ReadData('Data.xls','Sheet1','A1:B10')
I get
??? Attempt to reference field of
non-structure array.
So I am wondering what is the proper way to do this in a function?
`
I'm not sure why you're getting that particular error. However, when you do this:
fileID = 'Data.xls';
Sheet = 'Sheet1';
Lines = 'A1:B10';
data = xlsread(fileID,Sheet,Lines);
Then you are passing those variables into the function, and you shouldn't surround them with ''.
fileID = the string variable, 'Data.xls'
'fileID' = a string which contains the text 'fileID', and has no connection to the file you're trying to open.