Importing Many Files at The Same Time and Adding Id Indicator

How to import multiple .csv files at once?

Something like the following should result in each data frame as a separate element in a single list:

temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)

This assumes that you have those CSVs in a single directory--your current working directory--and that all of them have the lower-case extension .csv.

If you then want to combine those data frames into a single data frame, see the solutions in other answers using things like do.call(rbind,...), dplyr::bind_rows() or data.table::rbindlist().

If you really want each data frame in a separate object, even though that's often inadvisable, you could do the following with assign:

temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))

Or, without assign, and to demonstrate (1) how the file name can be cleaned up and (2) show how to use list2env, you can try the following:

temp = list.files(pattern="*.csv")
list2env(
lapply(setNames(temp, make.names(gsub("*.csv$", "", temp))),
read.csv), envir = .GlobalEnv)

But again, it's often better to leave them in a single list.

Import many .txt files, combine them into one data frame along with file names

If you use a for loop then you can add the numerical label column you want:

files.list <- list.files(pattern = ".txt")
df <- data.frame(observed=integer(),
simulated=integer(),
ID=character(),
stringsAsFactors=FALSE)

for (i in 1: length(files.list)) {
df.next <- read.table(files.list[[i]], header=TRUE)
df.next$ID <- paste0('simu', i)
df <- rbind(df, df.next)
}

Add filename column to table as multiple files are read and bound

I generally use the following approach, based on dplyr/tidyr:

data = tibble(File = files) %>%
extract(File, "Site", "([A-Z]{2}-[A-Za-z0-9]{3})", remove = FALSE) %>%
mutate(Data = lapply(File, read_csv)) %>%
unnest(Data) %>%
select(-File)

Sorting/condensing large CSV file by multiple columns

I managed to work out a solution with pandas. The code I used was:

import pandas as pd

df=pd.read_csv("data.csv",sep=",",usecols=["Segment ID","Date Time","indicator value"])

df['Date Time'] = ['AM' if '06' <= x[11:13] < '10'
else 'IP' if '10' <= x[11:13] < '16'
else 'PM' if '16' <= x[11:13] < '19'
else 'OP' if '19' <= x[11:13] or x[11:13] < '06'
else 'Error' for x in df['Date Time']]

grouped = df.groupby(['Segment ID','Date Time']).mean()

grouped.to_csv('output.csv', sep =',')

I cannot recommend Pandas enough.

Create file but if name exists add number

In a way, Python has this functionality built into the tempfile module. Unfortunately, you have to tap into a private global variable, tempfile._name_sequence. This means that officially, tempfile makes no guarantee that in future versions _name_sequence even exists -- it is an implementation detail.
But if you are okay with using it anyway, this shows how you can create uniquely named files of the form file#.pdf in a specified directory such as /tmp:

import tempfile
import itertools as IT
import os

def uniquify(path, sep = ''):
def name_sequence():
count = IT.count()
yield ''
while True:
yield '{s}{n:d}'.format(s = sep, n = next(count))
orig = tempfile._name_sequence
with tempfile._once_lock:
tempfile._name_sequence = name_sequence()
path = os.path.normpath(path)
dirname, basename = os.path.split(path)
filename, ext = os.path.splitext(basename)
fd, filename = tempfile.mkstemp(dir = dirname, prefix = filename, suffix = ext)
tempfile._name_sequence = orig
return filename

print(uniquify('/tmp/file.pdf'))

How can we convert a nested XML to CSV in Python Dynamically, Nested XML may contain array of values as well?

ElementTree is not really the best tool for what I believe you're trying to do. Since you have well-formed, relatively simple xml, try using pandas:

import pandas as pd

#from here, it's just a one liner
pd.read_xml('input.xml',xpath='.//store').to_csv('output.csv',sep=',', index = None, header=True)

and that should get you your csv file.

Conflict: Multiple assets emit to the same filename

i'm not quite familiar with your approach so I'll show you a common way to help you out.

First of all, on your output, you are specifying the filename to app.js which makes sense for me that the output will still be app.js. If you want to make it dynamic, then just use "filename": "[name].js".

The [name] part will make the filename dynamic for you. That's the purpose of your entry as an object. Each key will be used as a name in replacement of the [name].js.

And second, you can use the html-webpack-plugin. You don't need to include it as a test.

HTML input file selection event not firing upon selecting the same file

Set the value of the input to null on each onclick event. This will reset the input's value and trigger the onchange event even if the same path is selected.

var input = document.getElementsByTagName('input')[0];

input.onclick = function () {
this.value = null;
};

input.onchange = function () {
console.log(this.value);
};
<input type="file" value="C:\fakepath">


Related Topics



Leave a reply



Submit