Replace column values from a csv file with batch
You need all the tokens (for writing the modified file), not just the second one:
for /f "tokens=1,2,* delims=," %%A in (%srcfile%) do echo %%A,testuser,%%C
(where *
is "the rest of the line, undelimited"). %%B
would be the username, so just write the replacement string instead.
You could use an if
statement to process the first line differently, or you process it separately:
<"%srcfile%" set /p header=
(
echo %header%
for /f "skip=1 tokens=1,2,* delims=," %%A in (%srcfile%) do echo %%A,testuser,%%C
) > "%outfile%"
Change specific value in CSV file via Python
This is the solution opening the csv file, changing the values in memory and then writing back the changes to disk.
r = csv.reader(open('/tmp/test.csv')) # Here your csv file
lines = list(r)
Content of lines:
[['Ip', 'Sites'],
['127.0.0.1', '10'],
['127.0.0.2', '23'],
['127.0.0.3', '50']]
Modifying the values:
lines[2][1] = '30'
Content of lines:
[['Ip', 'Sites'],
['127.0.0.1', '10'],
['127.0.0.2', '30'],
['127.0.0.3', '50']]
Now we only have to write it back to a file
writer = csv.writer(open('/tmp/output.csv', 'w'))
writer.writerows(lines)
How to replace values of a columns of CSV in R shiny
You just need to add the fileUploader
logic to fill my_data
accordingly:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File", accept = ".csv"),
checkboxInput("header", "Header", TRUE),
selectInput("col", "Column to search:", NULL),
textInput("old", "Replace:"),
textInput("new", "By:"),
actionButton("replace", "Replace!"),
),
mainPanel(
DTOutput("table1")
)
)
)
server <- function(input, output, session) {
my_data <- reactiveVal(NULL)
observeEvent(input$file1, {
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
my_data(read.csv(file$datapath, header = input$header))
updateSelectInput(session, "col", choices = names(my_data()))
})
observeEvent(input$replace, {
req(input$col)
dat <- req(my_data())
traf <- if (is.numeric(dat[[input$col]])) as.numeric else identity
my_data(dat %>%
mutate(!!rlang::sym(input$col) :=
replace(!!rlang::sym(input$col),
as.character(!!rlang::sym(input$col)) == input$old,
input$new) %>%
traf()))
})
output$table1 <- renderDT(
req(my_data())
)
}
Replace csv file first column with list values in python
Just use loc
to modify the one column of your dataframe:
example = pd.DataFrame({0: [1, 2, 3],
2: ["a", "b", "c"]})
replacement_list = ["ab", "cd", "ef"]
example.loc[:, 2] = replacement_list
print(example)
0 2
0 1 ab
1 2 cd
2 3 ef
I encourage you to look at the doc about the behaviour of loc
and about indexing/selecting part of your dataframe.
Of course, it will work as intended if your list has the same size of the number of rows of the dataframe. If not, you can convert your list into a series first to handle the missing data.
How to replace value of a column converted to day and month to a text using Python?
You can use pandas.to_datetime
:
new["Room"]= (
pd.to_datetime(new["Room"], format="%d-%b", errors="coerce")
.dt.strftime("%#m-%#d")
.fillna(new["Room"])
)
Example :
col
0 1-Feb
1 2-Feb
2 3-Mar
3 2-1
Gives :
col
0 2-1
1 2-2
2 3-3
3 2-1
NB: In case you're facing errors, you need to give a minimal reproducible example to show how look like your original (.csv
) opened in a text editor and not in Excel.
Replace 'space' in certain value of csv columns
Select 'Attachment' columns using filter
and replace
all whitespaces by '%20' then update
your dataframe in place:
df.update(df.filter(like='Attachment').replace(' ', '%20', regex=True))
My advise if you need to escape HTML entities is to use quote
from urllib
module:
from urllib.parse import quote
df.update(df.filter(like='Attachment').fillna('').applymap(quote))
Update
Try:
out = df.filter(like='Attachment').unstack().str.split(';').explode()
out = out.where(~(out.str.startswith('file://').fillna(False)),
out.str.replace(' ', '%20'))
df.update(out.dropna().groupby(level=[0, 1]).apply(';'.join).unstack(0))
Related Topics
How The File Size Is Limited on a Specific File System
Can't Install Using Yum in Rhel 7.1
Is It Secure to Rely on "X-Forwarded-For" to Restrict Access by Ip in Apache While Using Cloudflare
Bash Command Line Arguments Passed to Sed via Ssh
Do Here-Strings Undergo Word-Splitting
Linux Directory Starting with Dot
Ld_Library_Path: How to Find a Shared Object
Does Chrome 12 Really Support CSS 3D Transforms? Including on Linux
Git Error: Cannot Handle Https
How to Use Opengl Without a Window Manager in Linux
How to Add a Custom System Call on X86 Ubuntu Linux
Is There a Rpm File Naming Convention in Lsb
Echo - Syntax Error: Bad Substitution