All,
This is a snippet of a text file that I need to change into a csv file.
|head1|head2|head3|
+----+------+-----+
|10000|10001|10002|
So I've used this python code to make it into a CSV.
#open the input & output files.
inputfile = open('tr2796h_05.10.txt', 'rb')
csv_file = r"mycsv1.csv"
out_csvfile = open(csv_file, 'wb')
#read in the correct lines
my_text = inputfile.readlines()[63:-8]
#convert to csv using | as delimiter
in_txt = csv.reader(my_text, delimiter = '|')
#hook csv writer to output file
out_csv = csv.writer(out_csvfile)
#write the data
out_csv.writerows(in_txt)
#close up
inputfile.close()
out_csvfile.close()
The output is this:
,head1,head2,head3,
,+----+------+-----+,
,10000,10001,10002,
as expected.
Question is this - how do I delete that second row?
Write the headers, skip a row, and then write the remaining rows.
out_csv.writerow(next(in_txt)) # headers
next(in_text) # skip
out_csv.writerows(in_txt) # write remaining
在my_text = inputfile.readlines()[63:-8]
之后添加del my_text[1]
my_text = inputfile.readlines()[63:-8]
。
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.