Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python, I need help with glob. I have a lot of data text files and want to order them. However glob makes it in the

Python, I need help with glob. I have a lot of data text files and want to order them. However glob makes it in the wrong order.

Have:

data00120.txt

data00022.txt

data00045.txt

etc

Want:

data00000.txt

data00001.txt

data00002.txt

etc

Code piece:

def last_9chars(x):

return(x[-9:])

files = sorted(glob.glob('data*.txt'),key = last_9chars)

whole code:

import numpy as np

import matplotlib.pyplot as plt

import glob

import sys

import re

from prettytable import PrettyTable

def last_9chars(x):

return(x[-9:])

files = sorted(glob.glob('data*.txt'),key = last_9chars)

x = PrettyTable()

x.field_names = ['DataNum', 'Mean', 'Standard Deviation']

filecount = 0

n = 0

maxfilecount = int(sys.argv[1]) if len(sys.argv) > 1 else len(files)

for f in files:

filecount +=1

my_data = np.loadtxt(f, delimiter='\t')

mean = np.mean(my_data[1])

std = np.std(my_data[1])

print(f"Here {n}")

x.add_row([f"{f}", mean, std])

n +=1

if filecount >= maxfilecount:

break

print(x)

data = x.get_string()

with open('Data.csv', 'w') as f:

f.write(data)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Describe the seven standard parts of a letter.

Answered: 1 week ago

Question

Explain how to develop effective Internet-based messages.

Answered: 1 week ago

Question

Identify the advantages and disadvantages of written messages.

Answered: 1 week ago