$z = Import-Csv zerotrac.csv $nums= Import-Csv allleetcode.csv $md=@{} #converting one csv into hashmap for quicker search foreach($r in $nums){ $md.add($r.frontendQuestionId,$r) } foreach($r in $z){ $nr=$md[$r.id] if ($nr){ $r | Add-Member -MemberType NoteProperty -Name "isPaidOnly" -Value $nr.isPaidOnly $r | Add-Member -MemberType NoteProperty -Name "difficulty" -Value $nr.difficulty $r | Add-Member -MemberType NoteProperty -Name "topicTags" -Value $nr.topicTags } } $z | Export-Csv -Path "C:\temp\newzerotrac1.csv" -NoTypeInformation
Showing posts with label csv. Show all posts
Showing posts with label csv. Show all posts
Wednesday
add new fields to csv file using powershell
powershell compare two csv files and output differences
I had to compare data returned from two APIs - one JSON another XML so
1. I used this viewer to convert json to csv and this app to save xml to csv
2. I used this PowerShell to show difference between two csv files:
Start-Transcript -Path result-10.csv $mas = Import-Csv 1.csv $marko = Import-Csv 2.csv $md=@{} #converting one csv into hashmap for quicker search foreach($r in $mas){ foreach($a in $r.psobject.properties){ $fn=$a.name $val=$a.value $md.Add($fn.ToLower(),$val) } } echo "fieldName,csv1,csv2" foreach($r in $marko){ foreach($a in $r.psobject.properties){ $k=$a.name.replace("_","").toLower() if ($md.ContainsKey($k)){ if ($md[$k] -ne $a.value){ echo "$($a.name),$($a.value),$($md[$k])" } } } } Stop-Transcript
IDataReader into csv
/// <summary>
/// Method for converting reader into CSV
/// </summary>
/// <param name="dataReader"></param>
/// <param name="includeHeaderAsFirstRow"></param>
/// <param name="separator"></param>
/// <returns></returns>
public Stream ToCSV(IDataReader dataReader, bool includeHeaderAsFirstRow, string separator)
{
Stream csv = new TempFileStream();
StreamWriter csvRows = new StreamWriter(csv);
StringBuilder sb = null;
if (includeHeaderAsFirstRow)
{
sb = new StringBuilder();
for (int index = 0; index < dataReader.FieldCount; index++)
{
if (dataReader.GetName(index) != null)
sb.Append(dataReader.GetName(index));
if (index < dataReader.FieldCount - 1)
sb.Append(separator);
}
csvRows.WriteLine(sb.ToString());
}
while (dataReader.Read())
{
sb = new StringBuilder();
for (int index = 0; index < dataReader.FieldCount - 1; index++)
{
if (!dataReader.IsDBNull(index))
{
string value = dataReader.GetValue(index).ToString();
if (dataReader.GetFieldType(index) == typeof(String))
{
//If double quotes are used in value, ensure each are replaced but 2.
if (value.IndexOf("\"") >= 0)
value = value.Replace("\"", "\"\"");
//If separtor are is in value, ensure it is put in double quotes.
if (value.IndexOf(separator) >= 0)
value = "\"" + value + "\"";
}
sb.Append(value);
}
if (index < dataReader.FieldCount - 1)
sb.Append(separator);
}
if (!dataReader.IsDBNull(dataReader.FieldCount - 1))
sb.Append(dataReader.GetValue(dataReader.FieldCount - 1).ToString().Replace(separator, " "));
csvRows.WriteLine(sb.ToString());
}
csvRows.Flush();
dataReader.Close();
return csv;
}
Subscribe to:
Posts (Atom)
make ubuntu business casual
to make ubuntu business casual - make it black remove background: gsettings set org.gnome.desktop.background picture-options ...
-
2010-11-24 Update: Please download latest version of Pidgin , that has this problem fixed , no additional steps required. Here are 3 ways to...
-
get ez_setup.py and setuptools-0.6c9-py2.6.egg . type: python ez_setup.py setuptools-0.6c9-py2.6.egg and that should get setuptools install...
-
Tested on windows only. Requirements: 1. python installed. 2. Google Data APIs - gdata-python-client can be downloaded from here : htt...