GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Python data reformat assistance
Operating System - HP-UX
1868008
Members
1371
Online
110516
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
- Integrity Servers
- Server Clustering
- HPE NonStop Compute
- HPE Apollo Systems
- High Performance Computing
Knowledge Base
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Knowledge Base
Forums
Discussions
- Cloud Mentoring and Education
- Software - General
- HPE OneView
- HPE Ezmeral Software platform
- HPE OpsRamp Software
Knowledge Base
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2020 10:55 AM
11-10-2020 10:55 AM
Python data reformat assistance
Hello,
I wonder if someone can provide some Python assistance. I have a simple requirement:
1. Print a line with UTC
2. Print any lines that follow, in this case those terminating with 1, 2 and 3
3. Prefix the lines (referenced in point 2) with the data segment enclosed in square brackets (referenced in point 1).
4. Please refer to the 'datafile' and 'Desired output' in the box below
5. My routine gives a partial result.
Thanks in advance
cat datafile
line...
line...
line...
[20/03/2020 10:38:456 UTC] Rest of the line
more information ...1
more information ...2
more information ...3
[21/03/2020 11:48:457 UTC] Rest of the line
more information ...5
more information ...6
more information ...7
$ cat reformat.py
#!/usr/bin/python3
outfile = '/tmp/datafile'
count = 0
with open(outfile, 'r') as fh:
for line in fh:
if line.find('UTC') != -1:
firstLine = line
stub = line.split(']')
newStub = stub[0]+']'
print(firstLine, end='')
elif line.find('more') != -1:
while count <3:
print(newStub,line, end='')
count += 1
break
$ python3 reformat.py
[20/03/2020 10:38:456 UTC] Rest of the line
[20/03/2020 10:38:456 UTC] more information ...1
[20/03/2020 10:38:456 UTC] more information ...2
[20/03/2020 10:38:456 UTC] more information ...3
[21/03/2020 11:48:457 UTC] Rest of the line
Desired output:
[20/03/2020 10:38:456 UTC] Rest of the line
[20/03/2020 10:38:456 UTC] more information ...1
[20/03/2020 10:38:456 UTC] more information ...2
[20/03/2020 10:38:456 UTC] more information ...3
[21/03/2020 11:48:457 UTC] Rest of the line
[21/03/2020 11:48:457 UTC] more information ...5
[21/03/2020 11:48:457 UTC] more information ...6
[21/03/2020 11:48:457 UTC] more information ...7
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2026 Hewlett Packard Enterprise Development LP