KJ9
KJ9

Reputation: 191

How can work output using python?

I made some script for modify text.

But I can't make result. below is my script. i just begin study python.

I think my script didn't work because f = open('find_c_volume_show.txt', 'w')

please help me.

import sys
from itertools import islice

def next_n_lines(file_opened, N):
        return [x.strip() for x in islice(file_opened, N)]

field_line = 1
num = 0
N = 9
split_line = field_line / N

strings = ("Vserver", "Volume Name", "Used Size", "Available Size", "Volume Size", "Aggregate Name", "Space Saved by Storage Efficiency")
f = open('find_c_volume_show.txt', 'w')
for line in open("c_volume_show.txt"):
        if any(s in line for s in strings):
                field1,field2 = line.strip().split(':')
                field_line += 1
                f.write(field2 + '\n')
f.close()

f = open('find_c_volume_show.txt', 'w')
f.write("Vserver,Volume Name,Aggregate Name,Volume Size,Available Size,Used Size,Space Saved\n")
with open('find_c_volume_show.txt', 'w') as result:
        while num < split_line:
                num += 1
                lines = next_n_lines(result, N)
                f.write('{}'.format(','.join(lines)) +'\n' )
f.close()

below is my text file. text file has data multi data.

                                   Vserver Name: FAS8040-ZZZZ
                                    Volume Name: vol0
                                 Aggregate Name: Node1_aggr0
                                    Volume Size: 466.6GB
                                 Available Size: 436.2GB
                                Filesystem Size: 466.6GB
                        Total User-Visible Size: 466.6GB
                                      Used Size: 30.40GB
                                Used Percentage: 6%
                                      Node name: FAS8040-ZZZZ
              Space Saved by Storage Efficiency: 0B
         Percentage Saved by Storage Efficiency: 0%
                   Space Saved by Deduplication: 0B
                     Space Saved by Compression: 0B
          Percentage Space Saved by Compression: 0%
                       Total Physical Used Size: 22.37GB

I want result like below.

Vserver,Volume Name,Aggregate Name,Volume Size,Available Size,Used Size,Space Saved
FAS8040-ZZZZ,vol0,Node1_aggr0,466.6GB,435.7GB,30.92GB,0B
FAS8040-YYYY,vol0,Node2_aggr0,466.6GB,428.7GB,37.91GB,0B
FAS8040-XXXX,vol0,Node2_aggr0,466.6GB,428.7GB,37.91GB,0B

Upvotes: 1

Views: 500

Answers (3)

KJ9
KJ9

Reputation: 191

It's work. I change strings content.

Thank you so much.

strings = ("Vserver Name:", "Volume Name:", "Aggregate Name:", "Volume Size:", "Available Size:", "  Used Size:", "  Used Percentage:", "Node name:", "Space Saved by Storage Efficiency:")

with open('find_c_volume_show.txt', 'w') as f:
        f.write("Vserver,Volume,Aggregate,Total,Avail,Used,UsedP,Node,Saved\n")
        row = []
        for line in open("c_volume_show.txt"):
                if any(s in line for s in strings):
                        field1,field2 = line.strip().split(':')
                        row.append(field2.strip())
                if len(row) == len(strings):
                        f.write('{}'.format(','.join(row)) +'\n' )
                        row = []
f.close()

Upvotes: 0

Rick
Rick

Reputation: 45231

The new-ish pathlib module (available in Python >= 3.4) is a much, much easier way for reading and writing files than the traditional open() function. It is also great for working with path-like objects (both in Windows and for other OSes).

from pathlib import Path
afile = Path(r'c:\temp\foo.bar') # absolute path to file

To read a file located at a path, you can just grab the text directly from the Path object!

contents = afile.read_text()
content_lines = contents.split('\n')

...and WRITE text directly!

data = '\n'.join(content_lines)
afile.write_text(data) # overwrites existing file

You can also use its open method rather than the open function:

with afile.open() as f:
    dostuff(f)

The with statement is a context manager. It automatically "cleans up" by closing the file when you are done (no matter what happens- even if there is an error).

It's Paths - Paths all the way down

Here is more about the Path library copied from another of my answers.

To simplify: you can build up any path (directory and file path objects are treated exactly the same) as an object, which can be an absolute path object or a relative path object. You can use raw strings to make complex paths (i.e., r'string') and pathlib will be very forgiving. However, note that there are better ways to build up paths than raw strings (see further down).

Here are examples:

from pathlib import Path

Path(r'c:\temp\foo.bar') # absolute path
Path(r'c:/temp/foo.bar') # same absolute path
Path('foo.bar') # different path, RELATIVE to current directory
Path('foo.bar').resolve() # resolve converts to absolute path
Path('foo.bar').exists() # check to see if path exists

Note that if you're on Windows pathlib forgives you for using the "wrong slash" in the second example. See discussion at the end about why you should probably always use the forward slash.

Simple displaying of some useful paths- such as the current working directory and the user home- works like this:

# Current directory (relative):
cwd = Path() # or Path('.')
print(cwd)

# Current directory (absolute):
cwd = Path.cwd()
print(cwd)

# User home directory:
home = Path.home()
print(home)

# Something inside the current directory
file_path = Path('some_file.txt') # relative path; or 
file_path = Path()/'some_file.txt' # also relative path
file_path = Path().resolve()/Path('some_file.txt') # absolute path
print(file_path)

To navigate down the file tree, you can do things like this. Note that the first object, home, is a Path and the rest are just strings:

afile = home/'Documents'/'Project Documentation'/'file.txt' # or
afile = home.join('Documents','Project Documentation','file.txt')

Check to see if it is a file or a directory (and exists) this way:

afile.is_dir()
afile.is_file()

Make a new, empty file without opening it like this (silently replaces any existing file):

afile.touch()

To make the file only if it doesn't exist, use exist_ok=False:

try:
    afile.touch(exist_ok=False)
except FileExistsError:
    # file exists

Make a new directory (under the current directory, Path()) like this:

Path().mkdir('new/dir') # get errors if Path()/`new` doesn't exist
Path().mkdir('new/dir', parents=True) # will make Path()/`new` if it doesn't exist
Path().mkdir('new/dir', exist_ok=True) # errors ignored if `dir` already exists

Get the file extension or filename of a path this way:

afile.suffix # empty string if no extension
afile.stem # note: works on directories too

Use name for the entire last part of the path (stem and extension if they are there):

afile.name # note: works on directories too

Rename a file using the with_name method (which returns the same path object but with a new filename):

new_file = afile.with_name('file_new.txt')

You can iterate through all the "stuff' in a directory like so using iterdir:

all_the_things = list(Path().iterdir()) # returns a list of Path objects

Sidebar: backslashes (\)

Be careful when using backslashes in a path string, especially ending a path with a backslash. As with any string, Python will read that terminating backslash as an escape character even in raw input mode. Observe:

>>> r'\'
  File "<stdin>", line 1
    r'\'
       ^
SyntaxError: EOL while scanning string literal

So this will give a pretty cryptic error message if you are not aware of this issue:

>>> Path(r'C:\')
  File "<stdin>", line 1
    Path(r'\')
             ^
SyntaxError: EOL while scanning string literal

The reason for this error is that \' is assumed to be a single quotation in the string. This works fine: '\'' (the second single quotation ends the string).

If you insist on using backslashes, be sure to use raw input mode or you will run into problems. For example, the '\t' character represents a tab. So when you do this (without raw input):

>>> Path('C:\temp')

You are putting a tab character into your path. This is perfectly legal and Python won't complain until you do something that causes Windows to try turning it into a real Windows path:

>>> Path('C:\temp').resolve()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\temp'

This is also a very cryptic error if you do not know what is going on! Best to avoid the backslash characters altogether when messing about with paths.

Upvotes: 0

alvis
alvis

Reputation: 349

The problem is that each time you open file with open(filename,'w'), it gets erased. You can use "temporal" file with different name to store results of your first for loop, or I would suggest to aggregate content of each line to list, and then write it straight away.

Additionally, you have problems with your "split_line" value, it is always 0. I guess what you mean is len(strings).

Here is a code:

import sys

strings = ("Vserver", "Volume Name", "Used Size", "Available Size", "Volume Size", "Aggregate Name", "Space Saved by Storage Efficiency")

with open('find_c_volume_show.txt', 'w') as f:
    f.write("Vserver,Volume Name,Aggregate Name,Volume Size,Available Size,Used Size,Space Saved,Snapshot,Total Used Size\n")
    row = []
    for line in open("c_volume_show.txt"):
        if any(s in line for s in strings):
            field1,field2 = line.strip().split(':')
            row.append(field2)
            print(row)
        if len(row) == len(strings):
            f.write('{}'.format(','.join(row)) +'\n' )
            print(row)
            row = []

Upvotes: 1

Related Questions