I need to read the txt file, read_table. When reading a long sentence, everything works fine, except that it is greatly reduced and a three-spot appears at the remote part. it looks like this:

On Wednesday, March 7, the US reg ...

Is there any parameter in the read_table method that will make reading the file correct?

Here is the code:

import pandas as pd import numpy as np path=r"C:\Users\neir0\Downloads\Telegram Desktop\textout.txt" df=pd.read_table(path,header=None,encoding='utf-8') date=[] state=[] for index , row in df.iterrows(): x=8480 case_1=range(0,x,3) case_2=range(1,x,3) if "date :" in str(row) : date.append(row) elif 'content :' in str(row) : state.append(row) elif 'href :' in str(row): continue df=pd.DataFrame({'date':date,'content':state}) z=[] for p in df['date']: z.append(p) year=[] month=[] day=[] for w in z: w=str(w).split('T') t=str(w[0]).replace('-',' ').replace('date : ','').split(' ') year.append(t[4]) month.append(t[5]) day.append(t[6]) df=pd.DataFrame({'year':year,'month':month,'day':day,'content':state}) df.to_csv(r"C:\Users\neir0\Desktop\ez\bitcoin_analyzer\data\content.csv",index=False) 

I want to draw attention to the possible jamb - I convert the line of the file (Series) to the usual str (). I do this, to check the conditions

  • one
    Maybe only the output is clipped? - mkkik
  • No, I wrote to the file - Sahar Vkusni

1 answer 1

Pandas cuts off the output of long rows in columns when printing, but when writing to a file ( .to_csv() , to_sql() , etc.), the data is not truncated:

Demo:

 In [1]: df = pd.DataFrame({'text':['*' * 80] * 5}) In [2]: df Out[2]: text 0 **********************************************... 1 **********************************************... 2 **********************************************... 3 **********************************************... 4 **********************************************... 

Check the length of the lines:

 In [3]: df.text.str.len() Out[3]: 0 80 1 80 2 80 3 80 4 80 Name: text, dtype: int64 

How to customize output:

 In [4]: pd.options.display.max_colwidth=81 In [5]: pd.options.display.width=100 In [6]: df Out[6]: text 0 ******************************************************************************** 1 ******************************************************************************** 2 ******************************************************************************** 3 ******************************************************************************** 4 ******************************************************************************** 

UPDATE:

The problem you have in this line:

 str(w[0]).replace('-',' ').replace('date : ','').split(' ') 

When str (...) is called, the Series.__str__() method is called, which is intended for printing - it eventually cuts the string.

  • and if I turn a Series object into a str? how then to be? - Sahar Vkusni
  • try: df.to_csv(filename, index=False) and check in the file - MaxU
  • what does "how to be" mean? output settings are the same for DataFrame and for Series - MaxU
  • the same, only without indexes - Sahar Vkusni
  • You see, when you turn a Series into str, the trimmed string is saved - Sahar Vkusni