0

I'm a very beginning programmer. I've been trying to figure this out for a few days but can't seem to wrap my head around it.

I am working with txt files (they are generated by another program). The columns contain no titles (headers?). Values are seperated by tabs.

This is the base file:

Boom    IFG : telefonische contactname (D+3)    13
Hemiksem    IFG : bureelgesprek/huisbezoek (Tf contact + 3 D)   2
Hemiksem    IFG : telefonische contactname (D+3)    7
Niel    IFG : bureelgesprek/huisbezoek (Tf contact + 3 D)   2
Rumst   IFG : bureelgesprek/huisbezoek (Tf contact + 3 D)   1

How do I properly get them into python?

I have experimented a bit. I converted the txt into a csv and in Pycharm they look exactly the same. But they are not treated the same.

If I read the txt:

data = pandas.read_csv("q658.txt", sep='\t') 
print (data)

I get :

       Boom               IFG : telefonische contactname (D+3)  13
0  Hemiksem  IFG : bureelgesprek/huisbezoek (Tf contact + 3 D)   2
1  Hemiksem               IFG : telefonische contactname (D+3)   7
2      Niel  IFG : bureelgesprek/huisbezoek (Tf contact + 3 D)   2
3     Rumst  IFG : bureelgesprek/huisbezoek (Tf contact + 3 D)   1

If I read the csv:

data = pandas.read_csv("q658.csv", sep= '\t')
print (data)

                                                       Boom  ...  13
Hemiksem  IFG : bureelgesprek/huisbezoek (Tf contact + 3 D)  ... NaN
Hemiksem               IFG : telefonische contactname (D+3)  ... NaN
Niel      IFG : bureelgesprek/huisbezoek (Tf contact + 3 D)  ... NaN
Rumst     IFG : bureelgesprek/huisbezoek (Tf contact + 3 D)  ... NaN

[4 rows x 3 columns]

However, in both cases, if I set header to zero or none, I get:

ValueError: header must be integer or list of integers

How do I best get it into Python, and how do I get it to treat the first row the same as the others?

data = pandas.read_csv("q658.csv", sep= '\t', header='none')
print (data)

gives:

ValueError: header must be integer or list of integers
StefWS
  • 35
  • 5
  • 1
    Can share the header=None try ? Because that is the way https://stackoverflow.com/questions/29287224/pandas-read-in-table-without-headers – azro Jun 19 '21 at 08:09
  • @Corralien. Thank you. I did read that, but apparently I kept overlooking it. Apologies. – StefWS Jun 19 '21 at 18:10

1 Answers1

1

You have to replace 'none' (a string) by None (object)

data = pandas.read_csv("q658.csv", sep= '\t', header=None)

Read this : https://docs.python.org/3/library/constants.html#None

Corralien
  • 109,409
  • 8
  • 28
  • 52