If I have a.txt file with this content:
1020,"Balance",+10000
1030,"Something",-5000
How do I remove whats in the middle, so that the only thing im left with is
1020,+10000
1030,-5000
If it's always in the same index:
with open('yourfile.txt', 'r') as f:
lines = f.readlines()
output = []
for line in lines:
temp = line.split(",")
output.append(temp[0])
output.append(temp[2])
print(output)
I would approach it with a regex:
import re
string = "1030,\"Something\",-5000"
stripped = re.sub("[\"].*[\"]", "", string)
print stripped
This prints 1030,,-5000
from there you can remove one of the commas.
You could import the data into a dataframe using Pandas and then delete the second column like this.
import pandas as pd
df = pd.read_csv('example.txt', header=None)
del df[1]
print(df)
You can use csv module for this task:
import csv
def removeColumn(fn1,fn2,idx=1):
with open(fn1,"r") as csvfile1:
reader = csv.reader(csvfile1)
with open(fn2,"w") as csvfile2:
writer = csv.writer(csvfile2)
for row in reader:
writer.write(row[:idx] + row[:idx+1])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.