如何将 CSV 文件重新格式化为 Google 日历格式?
因此,经过一些研究后,我找到了将 CSV 文件导入所需的格式。
Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private
问题是,我正在使用的 CSV 导出的格式或顺序不正确,收集数据的最佳方法是什么?那个信息?这是我的一些来源。
名称、用户名、行类型、开始日期、开始时间、结束时间、结束日期、段开始日期、类型
“Smith, John J”,jjs,Shift,5/29/2011,9:30,17:30, 5/29/2011,5/29/2011,常规
“史密斯,约翰·J”,jjs,Shift,5/30/2011,13:30,17:30,5/30/2011,5/30/2011,常规的
Dim Name As String = ""
Dim UserName As String = ""
Dim Data As String = """Smith, John J"",jj802b,Shift,5/29/2011,9:30,17:30,5/29/2011,5/29/2011,Transfer"
For r As Integer = 1 To 10
Name = Data.Substring(0, Data.LastIndexOf(""""))
Data = Data.Remove(0, Data.LastIndexOf(""""))
UserName = Data.Substring(Data.LastIndexOf(""""), ",")
Next
So after doing some research I was able to find the format I need to get the CSV File into
Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private
The issue is, the CSV export I'm working with is not in the correct format or order, what is the best way to go about gathering that information? Here is a bit of my source.
Name,User Name,Row Type,Start Date,Start Time,End Time,End Date,Segment Start Date,Type
"Smith, John J",jjs,Shift,5/29/2011,9:30,17:30,5/29/2011,5/29/2011,Regular
"Smith, John J",jjs,Shift,5/30/2011,13:30,17:30,5/30/2011,5/30/2011,Regular
Dim Name As String = ""
Dim UserName As String = ""
Dim Data As String = """Smith, John J"",jj802b,Shift,5/29/2011,9:30,17:30,5/29/2011,5/29/2011,Transfer"
For r As Integer = 1 To 10
Name = Data.Substring(0, Data.LastIndexOf(""""))
Data = Data.Remove(0, Data.LastIndexOf(""""))
UserName = Data.Substring(Data.LastIndexOf(""""), ",")
Next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是 Tim 的 DecodeCSV 的解决方案
Great Code
Following is the solution
Great Code for DecodeCSV by Tim
根据 CSV 文件的确切内容和格式保证,为了速度和方便,有时使用
split
on the,
是解析文件的最简单、最快的方法。 Your Name col 包含一个,
,它不是分隔符,这增加了一点复杂性,尽管假设名称始终包含 1,
,处理这种情况仍然很简单。有一些库可以解析 CSV 文件,这很有用。假设您不需要处理所有符合 CSV 规范的文件,我觉得它们有点矫枉过正。综上所述,您可以使用以下 正则表达式 轻松解析名为的 CSV 文件说服团体:
"(?<名称>[^"]+?)",(?<用户名>[^,]+?),(?<行类型>[^,]+?),(?< ;开始日期>[^,]+?),(?[^,]+?),(?[^,]+?),(?[^,]+?),(?[^,]+? ),(?<类型>\w+)
这将创建命名捕获组,然后您可以使用它们输出到新的 CSV 文件,如下所示:
请参阅 .NET Framework 正则表达式。
Depending on the exact content and guarantee of format of a CSV file, for speed and ease, sometimes using
split
on the,
is the easiest and fastest way to parse the file. Your Name col includes a,
that is not a delimiter, which adds a little bit of complication though it is still trivial to handle that case assuming the name always contains 1,
.There are libraries to parse CSV files, which can be useful. Assuming you don't need to handle all files that conform to the CSV spec I feel that they are overkill. With all that said you can use the following regular expression to easily parse the CSV file with named groups for convince:
"(?<Name>[^"]+?)",(?<UserName>[^,]+?),(?<RowType>[^,]+?),(?<StartDate>[^,]+?),(?<StartTime>[^,]+?),(?<EndTime>[^,]+?),(?<EndDate>[^,]+?),(?<SegmentStartDate>[^,]+?),(?<Type>\w+)
That will create named capture groups that you can then use to output to your new CSV file like so:
See .NET Framework Regular Expressions on MSDN for more information.
我想注意一些事情:
TextFieldParser
,您可以找到它在
FileIO
命名空间下工作使用输入 CSV。这使得
读取分隔文件更容易
而不是尝试处理常规问题
表达式和你自己的解析,
等等。
数据集我正在使用
List(Of
,或Dictionary(Of String, String))
相关词典列表
字符串到其他字符串。本质上
这与
DataTable
的访问模式和如果你对此感到更舒服
构建,欢迎您使用它
反而。词典列表
行为完全相同并且需要
设置少了很多,所以这里使用它
取而代之的是。
我承认其中一些是硬编码的,但如果您需要概括该过程,您可以将某些方面移至应用程序设置和/或更好地分解该功能。这里的重点是给您一个总体概念。该代码内联注释如下:
就其价值而言,使用示例数据似乎可以使用此代码在 OpenOffice.org Calc 中很好地打开输出文件。您希望为字段输出的格式由您决定,因此请修改
Select
中相应的Case
语句来执行此操作,祝您编码愉快!A few things I'd like to note:
TextFieldParser
, which you can findunder the
FileIO
namespace, to workwith the input CSV. This makes
reading delimited files a lot easier
than trying to deal with regular
expressions and your own parsing,
etc.
data sets I am using a
List(Of
, or aDictionary(Of String, String))
list of dictionaries that relate
strings to other strings. Essentially
this is not much different from the
access pattern of a
DataTable
andif you are more comfortable with that
construct, you're welcome to use it
instead. The list of dictionaries
behaved exactly the same and required
a lot less setup so it is used here
in its stead.
I admit some of this is hard-coded but if you need to generalize the procedure, you can move certain aspects out to application settings and/or better decompose the function. The point here was to give you a general idea. The code is commented inline below:
For what it's worth, using your sample data seemed to result in the output file opening just fine in OpenOffice.org Calc using this code. The format of what you wish to output for the fields is up to you, so modify the appropriate
Case
statement in theSelect
to do so, and happy coding!