public static CSVFile LoadFromDataTable(DataTable DT, bool HasHeader = true) { CSVFile CSVF = new CSVFile(); if (HasHeader) { CSVLine L = new CSVLine(); foreach (DataColumn C in DT.Columns) { L.Columns.Add(C.ColumnName); } CSVF.Lines.Add(L); } foreach (DataRow R in DT.Rows) { CSVLine L = new CSVLine(); foreach (DataColumn C in DT.Columns) { L.Columns.Add(R[C.ColumnName].ToString()); } CSVF.Lines.Add(L); } return(CSVF); }
public void Read([NotNull] FileInfo fi) { try { FileName = fi.Name; using (var sr = new StreamReader(fi.FullName)) { TsName = fi.Name.Replace(".csv", ""); while (!sr.EndOfStream) { var s = sr.ReadLine(); if (!string.IsNullOrWhiteSpace(s)) { var arr = s.Split(';'); var cl = new CSVLine(arr[3]); for (var i = 4; i < arr.Length; i++) { if (!string.IsNullOrWhiteSpace(arr[i])) { var d = double.Parse(arr[i]); cl.Values.Add(d); } } Lines.Add(cl); } } } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { #pragma warning restore CA1031 // Do not catch general exception types Ex = ex; } }
/// <summary> /// This constructor takes the following string format as input: /// <code>"(id,created,employee(id,firstname,employeeType(id), lastname),location)"</code> /// I am assuming that ids are integers, and that created is a very simple /// 2019-01-31 style date string. /// </summary> /// <param name="inputString"></param> public EmployeeLocation(string inputString) { InnerAndOuterString inputWithoutParanthesis = ExtractFromParanthesis.fromDelimiter("", inputString); if (inputWithoutParanthesis == null) { // this would happen if there were no paranthesis or if the first character was not an opening paranthesis throw new ArgumentException("Employee location does not begin and end with paranthesis"); } InnerAndOuterString employeeAndInputWithoutEmployee = ExtractFromParanthesis.fromDelimiter("employee", inputWithoutParanthesis.inner); List <string> employeeLocationParts = CSVLine.toListOfLength(employeeAndInputWithoutEmployee.outer, 4); this.id = GetId.getId(employeeLocationParts.ElementAt(0)); try { this.created = DateTime.ParseExact(employeeLocationParts.ElementAt(1), "yyyy-MM-dd", CultureInfo.InvariantCulture); } catch (System.FormatException) { throw new ArgumentException("Date format not in proper format (yyyy-MM-dd)"); } this.employee = new Employee(employeeAndInputWithoutEmployee.inner); this.location = employeeLocationParts.ElementAt(3); }
public void Read([NotNull] FileInfo fi) { using (var sr = new StreamReader(fi.FullName)) { TsName = fi.Name.Replace(".csv", ""); while (!sr.EndOfStream) { var s = sr.ReadLine(); if (!string.IsNullOrWhiteSpace(s)) { var arr = s.Split(';'); var cl = new CSVLine(arr[3]); for (var i = 4; i < arr.Length; i++) { if (!string.IsNullOrWhiteSpace(arr[i])) { var d = double.Parse(arr[i]); cl.Values.Add(d); } } Lines.Add(cl); } } } }
static _DATA_TYPE_ GetDataFromCSVTable <_DATA_TYPE_>(IRogueUI ui, CSVTable table, Func <CSVLine, _DATA_TYPE_> fn, IDs skillID) { // get line for id in table. CSVLine line = FindLineForModel(table, skillID); if (line == null) { throw new InvalidOperationException(String.Format("skill {0} not found", skillID.ToString())); } // get data from line. _DATA_TYPE_ data; try { data = fn(line); } catch (Exception e) { throw new InvalidOperationException(String.Format("invalid data format for skill {0}; exception : {1}", skillID.ToString(), e.ToString())); } // ok. return(data); }
public void AddLineWithoutIdx(string line) { CSVLine new_line = new CSVLine(); new_line.data = line; new_line.values = new_line.data.Split(','); m_csv_data.Add(m_csv_data.Count, new_line); return; }
void Update() { if (readFinish) { // 可以类似访问数据库一样访问配置表中的数据 CSVLine line = CSVHelper.Instance().SelectFrom("csv_test").WhereIDEquals(10011); Debug.Log(line["name"]); readFinish = false; } }
public CSVLine WhereIDEquals(int id) { CSVLine result = null; if (!dataContainer.TryGetValue(id.ToString(), out result)) { Debug.LogError(string.Format("CSVTable WhereIDEquals: The line you want to get data from is not found. id:{0}", id)); } return(result); }
private void AddLine(string key, CSVLine line) { if (dataContainer.ContainsKey(key)) { Debug.LogError(string.Format("CSVTable AddLine: there is a same key you want to add. key = {0}", key)); } else { dataContainer.Add(key, line); } }
public static SkillData FromCSVLine(CSVLine line) { return(new SkillData() { NAME = line[1].ParseText(), VALUE1 = line[2].ParseFloat(), VALUE2 = line[3].ParseFloat(), VALUE3 = line[4].ParseFloat(), VALUE4 = line[5].ParseFloat() }); }
public void RemoveData() { if (m_csv_data.Count < 1) { return; } CSVLine lineData = m_csv_data[0]; m_csv_data.Clear(); m_csv_data[0] = lineData; number = 0; }
public Result Read() { Result result = new Result(); if (!File.Exists(_outputFile)) { return(result); } FileStream logFileStream = new FileStream(_outputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader logFileReader = new StreamReader(logFileStream); try { CSVLine csvLine = new CSVLine(); List <string> fields = new List <string>(); while (!logFileReader.EndOfStream) { string line = logFileReader.ReadLine(); ParseLine(line, fields); csvLine.SetAllFields(fields); csvLine.Directory = csvLine.Directory.RemoveFirst(Application.dataPath + "\\"); csvLine.Directory = csvLine.Directory.Replace("\\", "/"); bool sameAsPrevious = result.ResultEntries.Count > 0 && result.ResultEntries.Last().ProjectDirectory == csvLine.Directory && result.ResultEntries.Last().File == csvLine.File; ResultEntry entry; if (!sameAsPrevious) { entry = new ResultEntry(); entry.ProjectDirectory = csvLine.Directory; entry.File = csvLine.File; } else { entry = result.ResultEntries.Last(); } entry.Lines.Add($"{csvLine.LineNumber} {csvLine.LineContent}"); result.ResultEntries.Add(entry); } } finally { logFileReader.Close(); logFileStream.Close(); } return(result); }
public void AddLine(string line) { //CSVLine lineEnd = m_csv_data[m_csv_data.Count - 1]; //if (lineEnd.values.Length < 1) // return; string idxStr = Convert.ToString(m_csv_data.Count); CSVLine new_line = new CSVLine(); new_line.data = idxStr + line; new_line.values = new_line.data.Split(','); m_csv_data.Add(m_csv_data.Count, new_line); return; }
public void BuildFile(string filename, string head_string) { if (File.Exists(filename)) { File.Delete(filename); } src_filename = filename; m_csv_data.Clear(); CSVLine headData = new CSVLine(); headData.values = head_string.Split(','); headData.data = head_string; m_csv_data[0] = headData; number = 0; }
public void LoadFile(string file) { var L = File.ReadAllLines(file); foreach (var l in L) { string S = l.Trim(); if (S.Length > 0) { var CSVL = new CSVLine(); CSVL.Parse(S); Lines.Add(CSVL); } } }
/// <summary> /// This constructor takes the following string format as input: /// <code>"id,firstname,employeeType(id),lastname"</code> /// </summary> /// <param name="inputString"></param> public Employee(string inputString) { InnerAndOuterString employeeTypeAndEmployeeWithoutET = ExtractFromParanthesis.fromDelimiter("employeeType", inputString); if (employeeTypeAndEmployeeWithoutET == null) { throw new ArgumentException("employeeType missing or missing its following paranthesis"); } List <string> employeeParts = CSVLine.toListOfLength(employeeTypeAndEmployeeWithoutET.outer, 4); this.id = GetId.getId(employeeParts.ElementAt(0)); this.firstname = employeeParts.ElementAt(1); this.lastname = employeeParts.ElementAt(3); this.employeeType = new EmployeeType(employeeTypeAndEmployeeWithoutET.inner); }
public ActorData(CSVLine line) { NAME = line[1].ParseText(); PLURAL = line[2].ParseText(); SPD = line[3].ParseInt(); HP = line[4].ParseInt(); STA = line[5].ParseInt() * WorldTime.TURNS_PER_HOUR / 30; // spacetime scales; disconnected ATK = line[6].ParseInt(); DMG = line[7].ParseInt(); DEF = line[8].ParseInt(); PRO_HIT = line[9].ParseInt(); PRO_SHOT = line[10].ParseInt(); FOV = line[11].ParseInt() * WorldTime.TURNS_PER_HOUR / 30; // spacetime scales AUDIO = line[12].ParseInt() * WorldTime.TURNS_PER_HOUR / 30; // spacetime scales; disconnected SMELL = line[13].ParseInt(); SCORE = line[14].ParseInt(); FLAVOR = line[15].ParseText(); }
private static TrainCarData ReadTrainCarData(CSVLine line) { TrainCarData trainCarData = new TrainCarData() { Class = Enum.Parse <TrainCarClass>(line["Class"]), Type = Enum.Parse <TrainCarType>(line["Type"]), Name = line["Name"], HasElectricity = bool.Parse(line["HasElectricity"]), HasAirConditioning = bool.Parse(line["HasAirConditioning"]), HasHeating = bool.Parse(line["HasHeating"]), NumSeatsPerDivison = int.Parse(line["NumSeatsPerDivision"]), }; trainCarData.NumSeats = trainCarData.NumSeatsPerDivison * (trainCarData.Type == TrainCarType.Open ? 1 : 9); return(trainCarData); }
/// <summary> /// Create a new UDP/CSV receiver listening on the given IP address and port. /// </summary> /// <param name="IPAddress">The IP address to listen.</param> /// <param name="Port">The port to listen.</param> /// <param name="ServiceBanner">Service banner.</param> /// <param name="Splitter">An array of delimiters to split the incoming CSV line into individual elements.</param> /// <param name="ReceiverThreadName">The optional name of the UDP receiver thread.</param> /// <param name="ReceiverThreadPriority">The optional priority of the UDP receiver thread.</param> /// <param name="ReceiverThreadIsBackground">Whether the UDP receiver thread is a background thread or not.</param> /// <param name="PacketThreadsNameCreator">An optional delegate to set the name of the UDP packet threads.</param> /// <param name="PacketThreadsPriority">The optional priority of the UDP packet threads.</param> /// <param name="PacketThreadsAreBackground">Whether the UDP packet threads are background threads or not.</param> /// <param name="Autostart">Start the UDP receiver thread immediately.</param> public UDPCSVReceiver(IIPAddress IPAddress, IPPort Port, String ServiceBanner = DefaultServiceBanner, IEnumerable <String> Splitter = null, String ReceiverThreadName = "UDP receiver thread", ThreadPriority ReceiverThreadPriority = ThreadPriority.AboveNormal, Boolean ReceiverThreadIsBackground = true, Func <UDPPacket <IEnumerable <String> >, String> PacketThreadsNameCreator = null, ThreadPriority PacketThreadsPriority = ThreadPriority.AboveNormal, Boolean PacketThreadsAreBackground = true, Boolean Autostart = false) : base(IPAddress, Port, ServiceBanner, // Mapper delegate <= do not use! null, // MapReduce delegate <= will automatically be reduced to multiple events! (UDPReceiver, Timestamp, LocalSocket, RemoteSocket, Message) => Message.ToUTF8String(). Trim(). Split(LineEndings, StringSplitOptions.RemoveEmptyEntries). Select(CSVLine => CSVLine.Trim(). Split((Splitter != null) ? Splitter.ToArray() : DefaultSplitter, StringSplitOptions.None). Select(CSVElement => CSVElement.Trim())), ReceiverThreadName, ReceiverThreadPriority, ReceiverThreadIsBackground, PacketThreadsNameCreator, PacketThreadsPriority, PacketThreadsAreBackground, Autostart) { this._Splitter = (Splitter != null) ? Splitter.ToArray() : DefaultSplitter; }
public void Load(string filename) { src_filename = filename; StreamReader reader = new StreamReader(filename); int count = 0; while (!reader.EndOfStream) { var line = reader.ReadLine(); if (line.Length <= 3) { continue; } if (line[0] == '#') { continue; } var values = line.Split(','); if (values.Length < 2) { continue; } CSVLine line_data = new CSVLine(); line_data.values = values; line_data.data = line; if (count == 0) { keywords = values; } m_csv_data.Add(count, line_data); count++; } number = m_csv_data.Count - 1; reader.Close(); }
/// <summary> /// LoadCSV /// Loading Data From CSV File. /// </summary> /// <param name="CSVPath">DATA path for CSV Extension</param> public CSVModel LoadCSV(string CSVPath) { try { StreamReader reader = new StreamReader(File.OpenRead(CSVPath)); CSVModel csvModel = new CSVModel(); while (!reader.EndOfStream) { string line = reader.ReadLine(); if (!String.IsNullOrWhiteSpace(line)) { string[] fields = line.Split(','); if (fields.Length == 4) { var csvLine = new CSVLine(); csvLine.CityName = fields[0]; csvLine.CityCode = fields[1]; csvLine.DistrictName = fields[2]; csvLine.ZipCode = fields[3]; csvModel.CSVLines.Add(csvLine); } } } reader.Close(); reader.Dispose(); return(csvModel); } catch (Exception ex) { throw new Exception("CSV Loading Exception\r\n" + ex.Message); } }
private void readCSV() { string csvLine; StreamReader file; try { file = new StreamReader(this._filename); } catch (IOException ex) { throw new IOException(ex.Message, ex); } while ((csvLine = file.ReadLine()) != null) { _lineList.Add(csvLine); } foreach (string line in _lineList) { CSVLine newLine = new CSVLine(_separator, line); _csvLineList.Add(newLine); } if (_dataList != null) { this.goBind(); } }