我有一个我想要修改的.csv文件。这是文件的格式:
Id, UTMGridEast, UTMGridNorth, LocDate, LocTime, Species
此数据集使用UTM坐标,我想将它们转换为纬度/经度并将它们放回各自位置的.csv文件中。我已经能够转换UTM坐标,该部分已经排序了!
现在,我已经有了代码来分隔这些值并将它们添加到arraylist中。我现在需要做的就是编辑内容并将其写回.csv文件。
我的GUI只包含两个按钮,这是我目前的代码:
public partial class MainWindow : Window
{
private string _filename;
public MainWindow()
{
InitializeComponent();
}
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Dataset"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Commar Seperated Values (.csv)|*.csv" ; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
_filename = dlg.FileName;
txtFilePath.Text = _filename;
}
}
private void btnConvert_Click(object sender, RoutedEventArgs e)
{
ConvertToLatLong();
}
private void ConvertToLatLong()
{
GeoUTMConverter geoUtmConverter = new GeoUTMConverter();
TextWriter tw = new StreamWriter("starkey.txt");
string textFile = System.IO.File.ReadAllText(_filename);
List<string> lines = new List<string>(textFile.Split(new[] { Environment.NewLine }, StringSplitOptions.None));
for (int iLine = 0; iLine < lines.Count; iLine++)
{
List<string> values = new List<string>(lines[iLine].Split(new[] { "," }, StringSplitOptions.None));
for (int iValue = 0; iValue < values.Count; iValue++)
{
Console.WriteLine(String.Format("Line {0} Value {1} : {2}", iLine, iValue, values[iValue]));
if (iLine > 0)
{
geoUtmConverter.ToLatLon(Convert.ToDouble(values[1]), Convert.ToDouble(values[2]), 11, GeoUTMConverter.Hemisphere.Northern);
Double latitude = geoUtmConverter.Latitude;
Double longitude = geoUtmConverter.Longitude;
Console.WriteLine("Latitude: " + latitude + " Longitude: " + longitude);
//EDIT AND WRITE TO FILE HERE
}
}
}
}
}
我尝试创建一个新的arraylist并使用StreamWriter
来编写,但我似乎无法正确使用它。
非常感谢任何帮助,谢谢!
答案 0 :(得分:0)
您似乎缺少的部分是
values[1]=latitude;
values[2]=longitude;
tw.WriteLine(String.Join(",",values);
不要忘记最后关闭作家。