Reading an excel file, extracting each cell value as a string - python-3.x

I have following excel file:
I need to save each value in a list as strings.
I am new to python, I have already read the file in a dataframe. Please help further.
df = pd.read_excel(userfile, converters={'Agentcode':str})

Related

Parsing a long text string in Excel

I have a column of text data in a csv file that looks something like this for each row:
{variable_1=result1, variable2=null, variable_three=result_3, variable_4=[{var4.1=result4.1, var4.2=result4.2, var4.3=result4.3}], variable_5=null}
I am trying to write some formulas to ultimately have the data look like this in Excel:
raw data
variable_1
variable2
variable_three
var4.1
var4.2
var4.3
variable_5
{variable_1=result1, variable2=null, variable_three=result_3, variable_4=[{var4.1=result4.1, var4.2=result4.2, var4.3=result4.3}], variable_5=null}
result1
null
result_3
result4.1
result4.2
result4.3
null
The variable names will be the same for each run of the query that fetches this, but the results will vary in character length--which is why I formatted my example the way I did. There is multiple rows of this too.
What's the best way to go about this??
Edit: There are approx 30 variables in my actual data

Saving a complex dictionary into a excel file

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

Converting Issue date with download to CSV

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").

How to convert the following data to table format?

I have a 2 column data in the following format where Name is a string and Location is an array seperated with commas
Name------Location
John---------A,B,C,D
Paul----------E,F,G,H
I want to convert it to the following format
Name--------Location
John-----------A
John-----------B
John-----------C
John-----------D
Paul------------E
Paul------------F
Can someone suggested how can I achieve this ?
Thanks in advance.
There are many ways to accomplish this. One option is to use Split, along the following lines:
Location = "A,B,C,D"
LocationList = Split(Location, ",")
That will populate LocationList with an array of individual locations. You can then loop through the individual locations and generate your output.

How can I change a variable in a .txt template, if I have more than 5000 different values for this variable in an excel table?

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 _).

Resources