How to read txt file separated by "|" using Python with the Pandas library?

Asked

Viewed 1,121 times

1

I have a txt file with the following structure:

-----------------------------------
|SKU.   |DT.Compra|Dt.Chegada|SKTD|
|---------------------------------|
|100312 |20171202 |20180105  | 27 |
|89721  |20171202 |20180105  | 26 |
|192063 |20171205 |20180104  | 25 |
|182285 |20171205 |20180107  | 24 |
|32934  |20171210 |20180105  | 21 |
|43495  |20171202 |20180112  | 22 |
|4342   |20171202 |20180110  | 23 |
|3124   |20171204 |20180104  | 28 |
|43495  |20171207 |20180105  | 23 |

How can I get the data in dataframe form and use the data?

  • Would 20171202 be a timestamp? Do you know the format of this value so that it is possible to make the conversion?

  • Theoretically it is a string in 'Yyyymmdd format'

  • user139757, I found the answer. I will edit the question with the current question.

1 answer

3


You can use the module csv to parse automatically:

import csv
with open('seu_arquivo.txt', newline='') as f:
     # pular as linhas divisorias:
     f = (linha.strip('|') for linha in f if '---' not in linha) 
     cf = csv.DictReader(f, delimiter='|')
     df = pd.DataFrame(cf)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.