-
Nov 28th, 2021, 04:11 PM
#1
Thread Starter
Member
Replacing 1st field of each line in a csv file
I need to change the first field of each line of a CSV file.
I have searched a lot of info but can't out it together so I have no code to show.
Here is the CSV file it's name is scores2.csv.
Code:
121, Slim(YEL),Adkison, 91, 60, 12.5
113, Larry(YEL),Bolitho, 65, 91, 7.2
117, Mickey(YEL),Call, 80, 87, 6.1
115, Marj(GRN),Daniels, 91, 70, 17.5
114, Bob(YEL),Duszak, 85, 82, 9.2
118, Bernie(GRN),Gearald, 83, 78, 8.2
111, Gail(GRN),Hammond, 81, 60, 1.5
116, Howard(GRN),Hammond, 90, 94, 4.4
119, Sam(YEL),Jalbert, 88, 92, 6.9
120, Ray(GRN),Lebel, 81, 79, 5.1
122, Bob(GRN),Vargo, 91, 91, 4.3
112, Doug(YEL),Watson, 91, 39, 2.5
I need to change the first field into sequential numbers.
I know I could do it by reading it with notepad and replacing one at a time but this file will grow to 100 lines.
Any help to push me in the right direction would be appreciated.
-
Nov 28th, 2021, 04:29 PM
#2
Re: Replacing 1st field of each line in a csv file
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lines() As String = IO.File.ReadAllLines("C:\Users\Paul\Desktop\csv.txt")
For x As Integer = 0 To lines.Count - 1
Dim fields() As String = lines(x).Split(New String() {", "}, StringSplitOptions.None)
fields(0) = (x + 1).ToString
Dim line As String = String.Join(", ", fields)
lines(x) = line
Next
IO.File.WriteAllLines("C:\Users\Paul\Desktop\csv.txt", lines)
End Sub
That'll re-number as...
Code:
1, Slim(YEL),Adkison, 91, 60, 12.5
2, Larry(YEL),Bolitho, 65, 91, 7.2
3, Mickey(YEL),Call, 80, 87, 6.1
4, Marj(GRN),Daniels, 91, 70, 17.5
5, Bob(YEL),Duszak, 85, 82, 9.2
6, Bernie(GRN),Gearald, 83, 78, 8.2
7, Gail(GRN),Hammond, 81, 60, 1.5
8, Howard(GRN),Hammond, 90, 94, 4.4
9, Sam(YEL),Jalbert, 88, 92, 6.9
10, Ray(GRN),Lebel, 81, 79, 5.1
11, Bob(GRN),Vargo, 91, 91, 4.3
12, Doug(YEL),Watson, 91, 39, 2.5
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 28th, 2021, 06:30 PM
#3
Thread Starter
Member
Re: Replacing 1st field of each line in a csv file
I am a part time programmer for personal fun. I would never gotten it.
I still don't understand what it does but will be studying it to see if I can understand each line.
Thanks .Paul for your help
-
Nov 28th, 2021, 06:53 PM
#4
Re: Replacing 1st field of each line in a csv file
It reads the file into a string array, each element contains one line of your csv
For each line in that string array, it splits that line into another string array containing your fields
The first field is changed, then the line reconstructed, then put back in the lines array.
Finally, it writes the csv back to the fileā¦
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|