Python Forum
Print data in rows and columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print data in rows and columns
#1
I have make a script in python having below output.

Output:
Output:
('User ID' 'Hits', {'1982641': 10, '105350': 9, '1008313': 7, '2073270': 11, '2073279':10})
I want to sort by hits and display in row and columns, How could i do that:
Output:
Userid Hits 1008313 7 105350 9 1982641 10 2073279 10 2073270 11
Reply
#2
try pprint
import pprint
pprint = prettyprinter.pprint
pprint(print what you want)
https://docs.python.org/2/library/pprint.html
Reply
#3
you can use f-string (python 3.6 or newer) or format
using f-string:
print(f'{item1:width} {item2:width}')
using format:
print('{:10} {:10}'.format(item1, item2))
10 is column width vary as needed

replace item1, item2 with your data
Reply
#4
Hello,

making:

a = ('User ID', 'Hits', {'1982641': 10, '105350': 9, '1008313': 7, '2073270': 11, '2073279':10})

you can use c = sorted(a[2].iteritems(), key=lambda (k,v): (v,k)) to sorted the dict and then you can print using @Larz60+ comments.
Reply
#5
Below is my code, Logs file contains hundred thousands of values few of mentioned in previous post. As i have tried your method but unable to fetch it.

fobj = open("logs", "r")
text = fobj.read()
dict = re.findall(r'.*user_id=(\d*)', text, re.M)

test = {}

for item in dict:
  try:
    test[item] += 1
  except:
    test[item] = 1


print('User ID', 'Hits', test)
Reply
#6
(Nov-09-2018, 11:27 AM)Larz60+ Wrote: you can use f-string (python 3.6 or newer) or format
using f-string:
print(f'{item1:width} {item2:width}')
using format:
print('{:10} {:10}'.format(item1, item2))
10 is column width vary as needed

replace item1, item2 with your data

Hi Larz60,

I followed your point but unable to get exact output what is need. I have paste my code in previous post. Please look into it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  XML minidom "Pretty Print" Lost Data marksy95 2 1,174 Jun-15-2024, 11:09 AM
Last Post: Larz60+
  how do you style data frame that has empty rows. gsaray101 0 1,035 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  print(data) is suddenly invalid syntax db042190 6 3,071 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  Converting a json file to a dataframe with rows and columns eyavuz21 13 13,540 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,941 Dec-19-2022, 11:11 PM
Last Post: Stockers
  Extracting Data into Columns using pdfplumber arvin 17 15,398 Dec-17-2022, 11:59 AM
Last Post: arvin
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 3,744 Dec-12-2022, 08:22 PM
Last Post: jh67
  Check DataFrames with different sorting in columns and rows Foxyskippy 0 1,209 Nov-19-2022, 07:49 AM
Last Post: Foxyskippy
  How to keep columns header on excel without change after export data to excel file? ahmedbarbary 0 1,742 May-03-2022, 05:46 PM
Last Post: ahmedbarbary
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 2,111 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020