How to clear a text file without deleting it using groovy - groovy

I've a text file which I'll be using it to write content.. But every time before I write something to the file, I wish to clear the content without deleting the file..
How would I achieve the above? Any suggestions?

With text files you can simply set it to an empty string.
file.text = ''

I would use the setBytes method on java.io.File and provide it with an empty byte array:
file.bytes = new byte[0]
Passing an empty list also works, impressively.
file.bytes = []

Presumably, you want simply to overwrite the file with new content. To do that:
def content = ...
new File("test.txt").withWriter { writer ->
writer.write(content)
}
Note that File.withWriter will do all the usual housekeeping re: open/close file.

Related

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

save file from array using fs without seperator

I have problem to save array to file, I don't now way I always getting ',' separator in new file that I assuming means new row in array. So for example I have file
this is file
with text
to check
I read this file save to array make some modification and then I want to save this file.
fs.writeFile('to_save', file.map(function(x){return x + '\n'}));
When I do like that I always getting new file with ,
,this is file
,with text
,to check
when I try to remove firs letter using
fs.writeFile('to_save', file.map(function(x){return x = x.substring(1, x.length) + '\n'}));
It remove me t, w and t, so my question is how to get ride this separator?
Use join instead of map
fs.writeFile('to_save', file.join("\n"));
writeFile expects a string, a buffer, or an Uint8Array as data. It will implicitly call toString() on an array, which is why you'll get the commas.

Remove quotes from csv file using opencsv

I am trying to add changes data in a csv file:
This is the sample data:
DATE status code value value2
"2016-01-26","Subscription All","119432660","1315529431362550","0.0080099833517888"
"2016-01-26","Subscription All","119432664","5836995058433524","0.033825584764444"
"2016-01-26","Subscription All","119432664","8287300074499777","0.076913377834744"
"2016-01-26","Subscription All","119432664","14870697739968326","0.0074188355187426"
My code used to format the data:
CSVReader reader = new CSVReader(new FileReader(new File(fileToChange)), CSVParser.DEFAULT_SEPARATOR, CSVParser.NULL_CHARACTER, CSVParser.NULL_CHARACTER, 1)
info "Read all rows at once"
List<String[]> allRows = reader.readAll();
CSVWriter writer = new CSVWriter(new FileWriter(fileToChange), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER)
writer.writeAll(allRows)
writer.close()
The output i get is this, with extra quote added instead of removing it.
""2016-01-26"",""Subscription All"",""119432660"",""1315529431362550"",""0.0080099833517888""
""2016-01-26"",""Subscription All"",""119432664"",""5836995058433524"",""0.033825584764444""
""2016-01-26"",""Subscription All"",""119432664"",""8287300074499777"",""0.076913377834744""
""2016-01-26"",""Subscription All"",""119432664"",""14870697739968326"",""0.0074188355187426""
I want to remove the quotes.
Please can someone help.
Also, is it possible to change the date format to yyyymmdd instead of yyyy-mm-dd?
allRows.each { String[] theLine ->
String newDate = theLine[0].replaceAll('-', '')
String newline = theLine.eachWithIndex { String s, int i -> return i > 0 ? s : newDate}
writer.writeLine(newline)
}
Thanks
When you instantiated your CSVReader you told it to treat no characters as quotes, therefore it read the existing quotes as data and did not remove them.
When you told CSVWriter not to add any quotes it honored your request. However, the input data contained quote characters, and the convention for including quotes inside a string in CSV is to double the quotes. Thus the
string value
ABC"DEF
gets coded in CSV as
"ABC""DEF"
So the result you see is the combination of not removing the quotes on input (you told it not to) and then doubling the quotes on output.
To solve this change the input option from NULL_CHARACTER to DEFAULT_QUOTE_CHARACTER. However be aware that if any of your data actually contains embedded quotes or commas the resulting output will not be valid CSV.
Also I think this might be a valid bug report against OpenCSV. I believe that OpenCSV needs to inform you if it is about to generate invalid CSV when you told it to omit quotes, probably via a runtime exception. Although I suppose they might argue that you chose to work without a net and should accept whatever you get. Personally I go for the "principle of least surprise", which IMHO would be not to double quotes when the output is unquoted.
Because quotation in your CSVReader is set to CSVParser.NULL_CHARACTER " is treated as normal character which is part of read token. This causes your array to contain data in form:
["2016-01-26", "Subscription All", "119432660", "1315529431362550", "0.0080099833517888"]
rather than:
[2016-01-26, Subscription All, 119432660, 1315529431362550, 0.0080099833517888]
So try changing option from CSVParser.NULL_CHARACTER to either
'"'
CSVParser.DEFAULT_QUOTE_CHARACTER (it also stores '"').
CsvToBean csvToBean = new CsvToBeanBuilder(new StringReader(csv))
.withMappingStrategy(strategy)
.withIgnoreLeadingWhiteSpace(true)
.withSeparator(',')
.withIgnoreEmptyLine(true)
.withQuoteChar('\'')
.withQuoteChar('"')
.build();

Remove header row from CSV's

I have a directory with circa 3k CSV files containing various data, I need to collate these into a single file at some point, but first I need to remove all of the header rows from each file.
Usually for this I would collate the files, and then simply open in Excel, and filter to the header rows before deleting them all. Unfortunately, these sum to something around 9M rows, and Excel doesn't like that...
Can anybody think of a way around this? Preferably some sort of batch script that will run through all files in a directory.
Thanks in advance,
A.
The following assumes the first line of each file is the header line to be eliminated.
It will only work properly if none of the files contain the <TAB> character, and none of the files is too large. I can't remember the specifics, but at some point, MORE with redirected output will hang waiting for a keypress if the input file gets too large.
(for %F in (*.csv) do #more +1 "%F") >concat_csv.txt
I made sure to give the output file a different extension so that the command does not try to process the output! An alternative is to redirect the output to a CSV file but in a different folder.
If you want to use this in a batch file, then double up the percents (%F becomes %%F)
I am not sure this is what you are looking for... Here is one way to get rid of the duplicate headers in C#. The main purpose of the code is to store one header is string header and to read the files by skipping the first row (while (rdr.Peek() != -1)).
I have also used a dictionary to store the rows of each csv file. This will prevent duplicate rows in different csv files to be included (I am not sure if this function will be helpful in your case).
Imagine fname is a string array that contains the files you wish to merge.
Dictionary<string, string> dict = new Dictionary<string, string>();
string destinationFile = <write path of your destination file>;
string dir = <write path of your original directory>
string header = "";
if (dir.Length != 0)
{
foreach (string f in fnames)
{
using (StreamReader rdr = new StreamReader(dir + "\\" + f))
{
header = rdr.ReadLine();
while (rdr.Peek() != -1)
{
string ln = rdr.ReadLine();
string[] split_ln = ln.Split(',');
string value = (split_ln.Length != 2) ? string.Join(",", split_ln.Skip(1)) : split_ln[1];
dict.Add(split_ln[0], value);
}
}
}
using (StreamWriter wr = new StreamWriter(destinationFile))
{
wr.WriteLine(header);
foreach (var pair in dict)
{
wr.WriteLine("{0},{1}", Convert.ToString(pair.Key), pair.Value);
}
}
}

regular expression with matching empty string

I am working on a django project to modify database options in the file settings.py.I want to use regular expression to do it.
the options just like :
'PASSWORD':'123456',
so I have write a function,the code is following:
def config_item(self,data,item,value):
rStr = "'"+item+"':(\s)?'\w*'"
src = "'"+item+"': '"+value+"'"
res = re.sub(rStr,src,data)
return res
So I can call like this to modify password to '000000',
data = config_item(data,'PASSWORD','0000')
But when the source password is blank or dest password is blank ,it does not work.That is ,it does not match 'PASSWORD':'',
Are there some wrong with the regular expression.
How do I write it rightly.
Maybe try using '[^']*' instead of '\w*'
I think \w is a bit more strict.

Resources