I am reading a csv or txt file.
The csv or txt file has this format (can have several lines in the same format):
character , Int, character
And I want to change it to this format:
character , character , Int
and then save the new file as txt or csv.
I have this code to read a csv or txt file.
a_list=open(file)
for line in a_list:
#line[1], line[2] = line[2], line[1] # Tried this line but didnt work
print(line)
And I get this as result:
A,10,B
C,13,D
I want to change it to:
A,B,10
C,D,13
Whats the best way to do it?
SOLUTION
Using @DarrylG answer I changed the code to:
with open('edges.txt') as f:
mylist = [line.rstrip('\n') for line in f]
for line in mylist:
line = line.split(',')
line[1], line[2] = line[2], line[1]
line = ','.join(line)
print(line)