首先,我是C#
的新手所以我有一个包含11列和超过20,000行的CSV文件,它只是来自运动跟踪gps的一堆数据。我想要做的是能够从CSV中获取数据并将每个列加载到一个单独的集合中,但是我无法按照我想要的方式工作。
我已经花了很长时间来搜索如何正确地做到这一点,但是我已经成功地将这些janky代码聚集在一起,确实将所有数据加载到集合中,但只会让我将每个数据作为一个字符串加载到集合中(不是十进制或char,我需要一些[是的我尝试将集合作为十进制或char之前进行交换])。
所以我需要帮助的是能够实际将CSV文件中的数据作为我想要的数据类型加载到集合中,并且如果有一种简单的方法可以跳过前8个左右的那些只是标题的行。
我需要的数据类型列表如下(按声明的顺序) 十进制 十进制 烧焦 十进制 烧焦 十进制 串 串 十进制 十进制 串
以下是我目前正在使用的代码:
//Seprate class for all the collection declarations
public static class GPSdata
{
public static List<string> time = new List<string>(); //time (in seconds, advances by 0.2)
public static List<string> lat = new List<string>(); //Latitude
public static List<string> NS = new List<string>(); //North/South
public static List<string> lon = new List<string>(); //Longtitude
public static List<string> EW = new List<string>(); //East/West
public static List<string> knots = new List<string>(); //Speed in Knots
public static List<string> date = new List<string>(); //Date [ddmmyy]
public static List<string> sats = new List<string>(); //**No clue**
public static List<string> HDOP = new List<string>(); //Satelite Horizontal error
public static List<string> alt = new List<string>(); //Elevation (above msl)
public static List<string> rawSV = new List<string>(); //Space Vehicle
}
//Method for loading the CSV data into the collections
public void LoadCSV(string filepath)
{
using (StreamReader reader = new StreamReader(filepath))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
GPSdata.time.Add(values[0]);
GPSdata.lat.Add(values[1]);
GPSdata.NS.Add(values[2]);
GPSdata.lon.Add(values[3]);
GPSdata.EW.Add(values[4]);
GPSdata.knots.Add(values[5]);
GPSdata.date.Add(values[6]);
GPSdata.sats.Add(values[7]);
GPSdata.HDOP.Add(values[8]);
GPSdata.rawSV.Add(values[9]);
GPSdata.alt.Add(values[10]);
}
}
}
还有一个来自我正在阅读的文件的数据示例: 31350.2,3750.9188,S,14458.8652,E,7.98,50817,0,2.3,0,23 31350.4,3750.9204,S,14458.867,E,6.66,50817,0,2.3,0,23
答案 0 :(得分:0)
你的班级应该是这样的。 E / W为正或负经度,S / N为正或负纬度。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.csv";
static void Main(string[] args)
{
new GPSdata(FILENAME);
}
}
//Seprate class for all the collection declarations
public class GPSdata
{
public static List<GPSdata> data = new List<GPSdata>();
public TimeSpan time { get; set; } //time (in seconds, advances by 0.2)
public int latDegrees { get; set; } //Latitude
public int latMinutes { get; set; } //Latitude
public int latSeconds { get; set; } //Latitude
public string NS { get; set; } //North/South
public int lonDegrees { get; set; } //Longtitude
public int lonMinutes { get; set; } //Longtitude
public int lonSeconds { get; set; } //Longtitude
public string EW { get; set; } //East/West
public decimal knots { get; set; } //Speed in Knots
public DateTime date { get; set; } //Date [ddmmyy]
public int sats { get; set; } //**No clue**
public decimal HDOP { get; set; } //Satelite Horizontal error
public decimal alt { get; set; } //Elevation (above msl)
public int rawSV { get; set; } //Space Vehicle
public GPSdata() { }
public GPSdata(string filepath)
{
int lineNumber = 0;
StreamReader reader = new StreamReader(filepath);
string line = "";
while ((line = reader.ReadLine()) != null)
{
if (++lineNumber > 8)
{
try
{
string[] values = line.Split(',');
GPSdata gpsdata = new GPSdata();
GPSdata.data.Add(gpsdata);
gpsdata.time = new TimeSpan((long)(decimal.Parse(values[0]) * (decimal)1.0E07));
int latDecimalPoint = values[1].IndexOf(".");
gpsdata.latSeconds = int.Parse(values[1].Substring(latDecimalPoint + 1));
gpsdata.latMinutes = int.Parse(values[1].Substring(latDecimalPoint - 2, 2));
gpsdata.latDegrees = int.Parse(values[1].Substring(0, latDecimalPoint - 2));
gpsdata.NS = values[2];
int lonDecimalPoint = values[3].IndexOf(".");
gpsdata.lonSeconds = int.Parse(values[3].Substring(lonDecimalPoint + 1));
gpsdata.lonMinutes = int.Parse(values[3].Substring(lonDecimalPoint - 2, 2));
gpsdata.lonDegrees = int.Parse(values[3].Substring(0, lonDecimalPoint - 2));
gpsdata.EW = values[4];
gpsdata.knots = decimal.Parse(values[5]);
int dateLen = values[6].Length;
gpsdata.date = new DateTime(int.Parse(values[6].Substring(dateLen - 2)), int.Parse(values[6].Substring(0, dateLen - 4)), int.Parse(values[6].Substring(dateLen - 4, 2)));
gpsdata.sats = int.Parse(values[7]);
gpsdata.HDOP = decimal.Parse(values[8]);
gpsdata.rawSV = int.Parse(values[9]);
gpsdata.alt = decimal.Parse(values[10]);
}
catch (Exception ex)
{
Console.WriteLine("Error Line Number : '{0}', Text : '{1}'", lineNumber,line);
}
}
}
Console.ReadLine();
}
}
}
答案 1 :(得分:0)
听起来你在这里问了两个问题,将文本解析成其他数据类型,这在另一个答案中讨论过。这里有另一个问题,更详细地描述了这一点。 String Parsing in C#。您要问的第二部分是关于在csv文件中跳过标题信息。使用StreamReader上的ReadLine()方法跳过某些行,如下所示:
using (StreamReader reader = new StreamReader(filepath))
{
for(int i = 0; i<8; ++i){
reader.ReadLine();
}
while (!reader.EndOfStream)
{
// the stuff you are already doing
}
}
答案 2 :(得分:0)
每个物业的单独收集不是正确的方法 这可能是您正在寻找的:
def game_intro():
intro = True
gameDisplay.fill(white)
largeText = pygame.font.Font('freesansbold.ttf', 90)
TextSurf, TextRect = text_objects("Run Abush Run!", largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
mouse = pygame.mouse.get_pos()
if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
else:
pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))
smallText = pygame.font.Font('freesansbold.ttf' ,20)
textSurf, textRect = text_objects("START!", smallText)
textRect.center = ( (150+(100/2)), (450+(430/2)) )
gameDisplay.blit(textSurf, textRect)
pygame.draw.rect(gameDisplay, red, (550, 430, 100, 50))
pygame.display.update()
clock.tick(15)
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()