Solution For Bypassing a Step: Text To CSV Pandas

Viewed 54
  1. I read text file with header=None because first 6 lines are unnecessary and becoming obstacle in using '|' as delimiter. Because I need to convert text file in csv file.

  2. Then I need to convert that file into csv file and I again need to import 27evening.csv file by using delimiter '|'. Then only I can save df2 as final csv.

I don not want to save(as csv) after 4th step shown in figure and want to open userhistory_aam.txt by using delimiter "|". Here I don't want to generate unnecessary middle file(27evening.csv) Can you please provide alternative.

'''

import pandas as pd
import numpy as np
df = pd.read_csv("userhistory_aam[50][100]27May.txt", header = None)
df.columns = [''] * len(df.columns)
df.drop([0,1,2,3,4,6],0,inplace=True)
df.to_csv("27evening.csv", index = None)
df2 = pd.read_csv("27evening.csv", delimiter = '|')
df2.to_csv('final.csv')

'''

1 Answers

use skiprows in your read method.

df = pd.read_csv('data.txt',sep='|',skiprows=6)

data.txt.

 random text and data
# could also start with a #
more random text 
1|2|3|4|5|6|7
1|2|3|4|5|6|7
1|2|3|4|5|6|7
A|B|C|D|E|F|G # <-- data starts here.
a|b|c|d|e|f|g 

print(df)

   A  B  C  D  E  F   G
0  a  b  c  d  e  f  g

Note # <-- data starts here. is just for illustration, don't put that into your text file.

Related