Eliminate special characters from CSV with C# - c#

I am getting data from a CSV file through my Web Api with this code
private List<Item> items = new List<Item>();
public ItemRepository()
{
string filename = HttpRuntime.AppDomainAppPath + "App_Data\\items.csv";
var lines = File.ReadAllLines(filename).Skip(1).ToList();
for (int i = 0; i < lines.Count; i++)
{
var line = lines[i];
var columns = line.Split('$');
//get rid of newline characters in the middle of data lines
while (columns.Length < 9)
{
i += 1;
line = line.Replace("\n", " ") + lines[i];
columns = line.Split('$');
}
//Remove Starting and Trailing open quotes from fields
columns = columns.Select(c => { if (string.IsNullOrEmpty(c) == false) { return c.Substring(1, c.Length - 2); } return string.Empty; }).ToArray();
var temp = columns[5].Split('|', '>');
items.Add(new Item()
{
Id = int.Parse(columns[0]),
Name = temp[0],
Description = columns[2],
Photo = columns[7]
});
}
}
But the CSV file returned data with special characters instead of an apostrophe.
For example in the CSV file the are values such as There’s which should be "There's" or "John’s" which should be "John's".
This ’ is there instead of an apostrophe.
How do I get rid of this to just show my apostrophe.
This kind of data is being returned in
Name = temp[0],
Description = columns[2],

You can use the HttpUtility.HtmlDecode to convert the characters. Here's an example:
var withEncodedChars = "For example in the CSV file the are values such as There’s which should be There's or John’s which should be John's. This ’ is there instead of an apostrophe.";
Console.WriteLine(HttpUtility.HtmlDecode(withEncodedChars));
If you run this in a console app it outputs:
For example in the CSV file the are values such as There's which should be There's or John's which should be John's. This ' is there instead of an apostrophe.

Related

C# Read List values into Textboxes [duplicate]

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 5 years ago.
I've been stuck on this problem for a little while now. I'm able to read information from a bunch (100) Textboxes and save the data into a CSV file but reading that information back into the form has me a little befuddled, I'm only trying to load the first 11 strings to start with. I can load the CSV into a List but I can't seem to move that data from the list to my Textboxes. Is there something I'm missing with my approach?
public List<string> LoadCsvFile(string filePath)
{
var reader = new StreamReader(File.OpenRead(filePath));
List<string> searchList = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
searchList.Add(line);
for (int i = 0; i < 11; i++)
{
string date = searchList[i];
string dropdownindex = searchList[i];
LasttextBox.Text = searchList[i];
FirsttextBox.Text = searchList[i];
EmailtextBox.Text = searchList[i];
PhonetextBox.Text = searchList[i];
HometextBox.Text = searchList[i];
InfotextBox.Text = searchList[i];
PrimarytextBox.Text = searchList[i];
EmailtextBox.Text = searchList[i];
SecondaryEmailtextBox.Text = searchList[i];
}
}
return searchList;
}
The error I'm getting is:
System.ArgumentOutOfRangeException: 'Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index'
I appreciate any help you can provide.
You are confusing lines with fields, and you don't even really need the List based on what it looks like you are trying to do. It seems like you are trying to assign field values to your text boxes.
Assuming your csv file looks similar to below(You have the e-mail textbox duplicated by the way):
Date,DropDownIndex,LastName,FirstName,Email,Phone,Home,Info,Primary,Email,SecondaryEmail
You can use Linq to parse your CSV file into an anonymous class with all the values you want and then loop through and assign those to your text boxes
For example:
var csvRecords = File
.ReadAllLines(filePath)
.Select(c => c.Split(',')).Select(c => new
{
Date = c[0],
DropDown = c[1],
LastName = c[2],
FirstName = c[3],
Email = c[4],
Phone = c[5],
Home = c[6],
Info = c[7],
Primary = c[8],
Email2 = c[9],
SecondaryEmail = c[10]
}).ToList();
foreach (var csvRecord in csvRecords)
{
var date = csvRecord.Date;
var dropdownindex = csvRecord.DropDown;
LasttextBox.Text = csvRecord.LastName;
FirsttextBox.Text = csvRecord.FirstName;
EmailtextBox.Text = csvRecord.Email;
PhonetextBox.Text = csvRecord.Phone;
HometextBox.Text = csvRecord.Home;
InfotextBox.Text = csvRecord.Info;
PrimarytextBox.Text = csvRecord.Primary;
EmailtextBox.Text = csvRecord.Email2;
SecondaryEmailtextBox.Text = csvRecord.SecondaryEmail;
}
Some things you will need to consider is if your CSV file only contains comma delimiters and no double quote qualifiers as there could potentially be commas in the actual values which will then throw off the positions of your various fields. If you are still wanting to return a List via the method, you can't return the anonymous typed list, so you will need to create a class, and then instead of using the new in the Linq select, you would use new and assign the fields the same way.
This approach is more scalable, i.e., it will work with any number of csv file rows and/or text boxes so long as you have control over the names of the text boxes. This design ignores lines from the csv file that there is no text box for
public Form1()
{
InitializeComponent();
//simulate a list loaded from a csv file
IList<string> stringsFromCsv = new List<string>
{
"from csv line dog",
"from csv line cat",
"from csv line fish",
"from csv line frog",
"from csv line squirrel",
"from csv line turtle",
"from csv line squid",
"from csv line bass",
"from csv line tiger",
"from csv line lion"
};
//make a collection of all the controls in the groupbox (or form, or whatever)
Control.ControlCollection controls = groupBox1.Controls;
int listIndex = 0;
//loop based on the number of items from the csv file
while (listIndex <= stringsFromCsv.Count - 1)
{
//create a text box name from the current list index
string expectedTextBoxName = "textBox" + ((listIndex + 1).ToString());
//this is brute force, but step thru all the controls until
//you find a text box whose name matches
foreach (Control control in controls)
{
//skip the control if its not a text box
if (control.GetType().Name != "TextBox")
continue;
if (control.Name == expectedTextBoxName)
{
control.Text = stringsFromCsv[listIndex];
break;
}
}
listIndex = listIndex + 1;
if (listIndex > stringsFromCsv.Count - 1)
break;
}
}

How to delete all lines in text file that is NOT a number greater than a specific value? (Including strings)

My Issue:
I am looking into a text file and i need to delete all the lines, or use string.empty that does not meet the requirement. For example,
The requirement is 50000. There are words/text in the file and also numbers that are smaller than the requirement. How can i delete all line in a text file that do not meet the requirement? I do not want to be specific with the other values in the text file as they can differ.
My Research:
I have searched and found to delete specific values then rewrite them onto a new file which i am trying to do. I have found different ways that dont meet my needs. I am using what i have found below but am missing something to make it complete
My Effort:
DialogResult openFile = openFileDialog1.ShowDialog();
if (openFile == DialogResult.OK)
{
string file = openFileDialog1.FileName;
string content = File.ReadAllText(file);
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text File|*.txt";
sfd.FileName = "New Text Doucment";
sfd.Title = "Save As Text File";
if (sfd.ShowDialog() == DialogResult.OK)
{
string path = sfd.FileName;
StreamWriter bw = new StreamWriter(File.Create(path));
bw.WriteLine(content);
bw.Close();
File.WriteAllLines(path, File.ReadAllLines(path).Select(x => string.Format("{0},", x)));
string newContent = File.ReadAllText(path);
newContent = newContent.Remove(newContent.LastIndexOf(","));
File.WriteAllText(path, newContent);
string secondContent = File.ReadAllText(path);
int number = int.Parse(File.ReadAllText(path));
if (checkBox1.Checked == true)
{
secondContent = secondContent.Replace("BRUSH(1,0)", string.Empty);
secondContent = secondContent.Replace("REGION 1,", string.Empty);
secondContent = secondContent.Remove(secondContent.LastIndexOf(","));
File.WriteAllText(path, secondContent);
if (secondContent.Contains())
{
number = 0;
secondContent = secondContent.Replace(number.ToString(), string.Empty);
File.WriteAllText(path, secondContent);
}
}
else if (checkBox2.Checked == true)
{
secondContent = secondContent.Replace("BRUSH(1,0),", ")),");
secondContent = secondContent.Replace("REGION 1,", string.Empty);
secondContent = secondContent.Remove(secondContent.LastIndexOf(","));
File.WriteAllText(path, secondContent);
}
//Just trying
foreach (char c in secondContent)
{
if (secondContent.All(char.IsDigit))
{
char = string.Empty;
}
}
}
}
What i am doing above:
Grabbing an existing file,
adding the contents to a new file,
add a comma at the end of each line,
removing the last comma at the end of the file.
removing a line that contains brush or region
Now here are examples of the file:
TYPE_CODE char(2),
DESCRIPT0 char(25),
TYPE_COD0 char(3),
DESCRIPT1 char(36),
DATA,
BRUSH(1,0),
REGION(1,0),
13502,
319621.99946835 110837.002493295,
319640.501385461 110850.59860145,
319695.199120806 110879.700271183,
319728.303041127 110879.300385649,
319752.898058391 110876.501186912,
319767.401120868 110872.702274339,
The numbers at the bottom of that example is what the entire document should look like
To address just the issue stated try the following:
//Get the lines from the file
List<string> lines = System.IO.File.ReadAllLines("MyFile.txt").ToList();
//Removed the lines which are empty or when split using ' ' contain items other the numbers >= 50000
double d = 0;
lines.RemoveAll(x => string.IsNullOrWhiteSpace(x) || x.TrimEnd(',').Split(' ').Any(y => !double.TryParse(y, out d) || double.Parse(y) < 50000));
//Write the new file
System.IO.File.WriteAllLines("MyFile2.txt", lines.ToArray());
To add a comma to the end of each line add the following lines before saving:
//Remove any existing ',' and add our own
lines = lines.Select(x => x.TrimEnd(',') + ",").ToList();
//Remove any trailing ',' from the last line
lines[lines.Count - 1] = lines[lines.Count - 1].TrimEnd(',');
(Edited to handle multiple numbers per line)
(Fixed conversion errors)
(Fixed for existing ',')
First, abstraction would be your friend here. Break up your code into different peices to make it a little easier to handle. Plus when you have to change your requirements later, you will have an easy spot to do it.
Second, File.ReadLines is also your friend because it will give you a collection of strings, which will let you use LINQ.
Which brings me to your third friend, LINQ, which will let you query the collection of file lines.
public void FormatFile(string sourcePath, string targetPath)
{
IEnumerable<String> originalContent = GetFileLines(sourcePath);
IEnumerable<String> formatedContent = ProcessFileLines(originalContent);
OutputResults(targetPath, formatedContent);
}
public IEnumerable<String> GetFileLines(string path) {
return File.ReadLines(path);
}
public IEnumerable<string> ProcessFileLines(IEnumerable<string> fileLines)
{
// In this method you can process the logic that applies to the whole
// set of file lines (e.g. lineCount, removing empyLines, etc)
return processedLines.Select(l => ProcessLine(l)
.Where(l => !string.IsNullOrEmpty(l))
.GetRange(0, MAX_LINE_COUNT);
}
public string ProcessLine(string fileLine)
{
// In this method just focus on logic applied to each specific line.
string s = fileLine.Substring(0, 5);
if (s.Equals("BRUSH", StringComparison.InvariantCultureIgnoreCase)
return string.Empty;
return fileLine;
}
public void OutputResults(string targetPath, IEnumerable<string> fileLines)
{
string outputText = fileLines.Join($",{Environment.NewLine}");
File.WriteAllText(targetPath, outputText);
}
So basically you can just call FormatFile(#"C:\mySource", #"C:\myTarget"); and you will be good to go.

How to incorporate a text qualifier in a file name used as a variable?

I have a C# script which takes in two CSV files as input, combines the two files, performs numerous calculations on them, and writes the result in a new CSV file.
These two input CSV file names are declared as variables and are used in the C# script by accessing those variable names.
The data in the input CSV files looks like this:
Since the data has values in thousands and millions, line splits in the C# code are truncating the data incorrectly. For instance a value of 11,861 appears only as 11 and 681 goes in the next columns.
Is there any way in C#, by which I can specify a text qualifier (" in this case) for the two files ?
Here is the C# code snippet:
string[,] filesToProcess = new string[2, 2] { {(String)Dts.Variables["csvFileNameUSD"].Value,"USD" }, {(String)Dts.Variables["csvFileNameCAD"].Value,"CAD" } };
string headline = "CustType,CategoryType,CategoryValue,DataType,Stock QTY,Stock Value,Floor QTY,Floor Value,Order Count,Currency";
string outPutFile = Dts.Variables["outputFile"].Value.ToString();
//Declare Output files to write to
FileStream sw = new System.IO.FileStream(outPutFile, System.IO.FileMode.Create);
StreamWriter w = new StreamWriter(sw);
w.WriteLine(headline);
//Loop Through the files one by one and write to output Files
for (int x = 0; x < filesToProcess.GetLength(1); x++)
{
if (System.IO.File.Exists(filesToProcess[x, 0]))
{
string categoryType = "";
string custType = "";
string dataType = "";
string categoryValue = "";
//Read the input file in memory and close after done
StreamReader sr = new StreamReader(filesToProcess[x, 0]);
string fileText = sr.ReadToEnd();
string[] lines = fileText.Split(Convert.ToString(System.Environment.NewLine).ToCharArray());
sr.Close();
where csvFileNameUSD and csvFileNameCAD are variables with values pointing to their locations.
Well, based on the questions you have answered, this ought to do what you want to do:
public void SomeMethodInYourCodeSnippet()
{
string[] lines;
using (StreamReader sr = new StreamReader(filesToProcess[x, 0]))
{
//Read the input file in memory and close after done
string fileText = sr.ReadToEnd();
lines = fileText.Split(Convert.ToString(System.Environment.NewLine).ToCharArray());
sr.Close(); // redundant due to using, but just to be safe...
}
foreach (var line in lines)
{
string[] columnValues = GetColumnValuesFromLine(line);
// Do whatever with your column values here...
}
}
private string[] GetColumnValuesFromLine(string line)
{
// Split on ","
var values = line.Split(new string [] {"\",\""}, StringSplitOptions.None);
if (values.Count() > 0)
{
// Trim leading double quote from first value
var firstValue = values[0];
if (firstValue.Length > 0)
values[0] = firstValue.Substring(1);
// Trim the trailing double quote from the last value
var lastValue = values[values.Length - 1];
if (lastValue.Length > 0)
values[values.Length - 1] = lastValue.Substring(0, lastValue.Length - 1);
}
return values;
}
Give that a try and let me know how it works!
You posted a very similar looking question few days ago. Did that solution not help you?
If so, what issues are you facing on that. We can probably help you troubleshoot that as well.

csv file reading only first line

I'm trying to upload a series of client info's through a csv ,I had some trouble with this in the eginning but my previous post was answered so I was able to start reading in the data however it only reads the first line. Just wondering if anyone had any ideas. I've included the code below
private void btnUpload_Click(object sender, EventArgs e)
{
//Browse for file
OpenFileDialog ofd = new OpenFileDialog();
//Only show .csv files
ofd.Filter = "Microsoft Office Excel Comma Separated Values File|*.csv";
DialogResult result = ofd.ShowDialog();
//If the user selects a valid file
if (result == DialogResult.OK)
{
//File is delimited by a comma
char[] laClientDelim = { ',' };
//New object for string manipulation
objStringManipulation = new StringManipulation();
// Parse the csv file
List<string[]> lsClientList = objStringManipulation.parseCSV(ofd.FileName, laClientDelim);
foreach (string[] laClient in lsClientList)
{
//Create new object for manipulating the database
objSqlCommands = new SqlCommands("Client", "ClientName");
string[] records = File.ReadAllLines(ofd.FileName); // read the file completely line by line
char splitChar = ',';
int splitCharCount = 0;
int k = 0;
string[] fields = records[k].Split(splitChar); // reads all the single values per line. 'splitChar' should be the delimiter.
splitCharCount++;
if (splitCharCount >= 4 && splitCharCount <= 10)
{
var stuff = from l in File.ReadLines(ofd.FileName)
let x = l.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.Select(s => char.Parse(s))
select new
{
Client = x,
ClientName = x
};
}
//Inserts the client info into datbase
objSqlCommands.sqlCommandInsertorUpdate("INSERT", records[k]);//laClient[0]);
k++;
//Refreshs the Client table on display from the
this.clientTableAdapter.Fill(this.kIIDImplementationCalcDataSet.Client);
//MAKE SURE TO ONLY ADD IN CLIENT AND CLIENT NAME
//update the view
dgvClientlst.Update() ;
}
}
}
your loop looks like this essentially :
foreach (string[] laClient in lsClientList)
{
int k = 0;
string[] records = File.ReadAllLines(ofd.FileName);
string[] fields = records[k].Split(splitChar);
k++;
}
Your 'k' value never makes it past 0 for each laClient. You need to loop internally for each line.
I know my answer is not exactly what you are looking for but if you convert the .csv file to .xls file then you can manipulate it very easily. I have done this on multiple occasions and if you want I can provide you instructions with code.
Expanding on Jonesy's answer (because I can't yet comment), declare the variable k outside the loop. It's being reset to zero each time through.
int k = 0;
foreach (string[] laClient in lsClientList)
{
string[] records = File.ReadAllLines(ofd.FileName);
string[] fields = records[k].Split(splitChar);
k++;
}

using .replace to replace a word in text document (c#)

currently have the following code:
string[] fileLineString = File.ReadAllLines(Server.MapPath("~") + "/App_Data/Users.txt");
for (int i = 0; i < fileLineString.Length; i++)
{
string[] userPasswordPair = fileLineString[i].Split(' ');
if (Session["user"].ToString() == userPasswordPair[0])
{
userPasswordPair[i].Replace(userPasswordPair[1], newPasswordTextBox.Text);
}
}
}
the text file is set out as: 'username' 'password
what i'm trying to do is be able to edit the password and replace it with a new one using my code, but my code seems to do nothing and the text file just stays the same.
string[] fileLineString = File.ReadAllLines(Server.MapPath("~") + "/App_Data/Users.txt");
for (int i = 0; i < fileLineString.Length; i++)
{
string[] userPasswordPair = fileLineString[i].Split(' ');
if (Session["user"].ToString() == userPasswordPair[0])
{
// set the new password in the same list and save the file
fileLineString[i] = Session["user"].ToString() + " " + newPasswordTextBox.Text;
File.WriteAllLines((Server.MapPath("~") + "/App_Data/Users.txt"), fileLineString);
break; // exit from the for loop
}
}
At the moment, you're not storing the file.
Your replace is not assigned to a variable (Replace does not edit or write anything, it just returns the new string object).
Corrected code:
string[] fileLineString = File.ReadAllLines(Server.MapPath("~") + "/App_Data/Users.txt");
for (int i = 0; i < fileLineString.Length; i++)
{
string[] userPasswordPair = fileLineString[i].Split(' ');
if (Session["user"].ToString() == userPasswordPair[0])
{
fileLineString[i] = fileLineString[i].Replace(userPasswordPair[1], newPasswordTextBox.Text);
break;
}
}
File.WriteAllLines((Server.MapPath("~") + "/App_Data/Users.txt", fileLineString);
String _userName = "User";
String _newPassword = "Password";
// Reading All line from file
String _fileContent = System.IO.File.ReadAllLines("filePath").ToString();
// Pattern which user password like to changed
string _regPettern = String.Format(#"{0} ?(?<pwd>\w+)[\s\S]*?", _userName);
Regex _regex2 = new Regex(_regPettern, RegexOptions.IgnoreCase);
String _outPut = Regex.Replace(_fileContent, _regPettern, m => m.Groups[1] + " " + _newPassword);
// Writing to file file
System.IO.File.WriteAllText("filePath", _outPut);

Categories

Resources