输入python代码的查询

问题描述 投票:0回答:1

所以我为我的研究创建了这个代码,但我想将它用于大量的数据文件,我不想手动执行,这意味着在我的代码中重新输入一些行来使用所需的文件。如何在python中使用input命令(我在Windows操作系统上使用python 2.7)以更快地使用它,只需键入所需数据文件的名称即可。我的代码到目前为止:

import iodata as io
import matplotlib.pyplot as plt
import numpy as np
import time
from scipy.signal import welch
from scipy import signal

testInstance = io.InputConverter()
start = time.time()
conversionError = io.ConversionError()
#data = testInstance.convert(r"S:\Doktorat\Python\", 1", conversionError)
data = testInstance.convert(r"/Users/PycharmProjects/Hugo/20160401", "201604010000", conversionError)
end = time.time()
print("time elapsed " + str(end - start))

if(conversionError.conversionSucces):
    print("Conversion succesful")
if(conversionError.conversionSucces == False):
    print("Conversion failed: " + conversionError.conversionErrorLog)
print "Done!"

# Create a new subplot for two cannals 1 & 3
a = np.amin(data.data)
Bx = data.data[0,]
By = data.data[1,]
dt = float(300)/266350
Fs = 1/dt
t = np.arange(0,300,dt*1e3)
N = len(Bx)
M = len(By)
time = np.linspace(0,300,N)
time2 = np.linspace(0,300,M)

filename = 'C:/Users/PycharmProjects/Hugo/20160401/201604010000.dat'
d = open(filename,'rb')
degree = u"\u00b0"
headersize = 64
header = d.read(headersize)
ax1 = plt.subplot(211)
ax1.set_title(header[:16] + ', ' +                                  # station name
    'Canals: '+header[32:33]+' and '+header[34:35]+ ', '            # canals
    +'Temp'+header[38:43]+degree+'C'                                # temperature
    +', '+'Time:'+header[26:32]+', '+'Date'+' '+header[16:26])      # date

plt.ylabel('Pico Tesle [pT]')
plt.xlabel('Time [ms]')
plt.grid()
plt.plot(time[51:-14], Bx[51:-14], label='Canal 1', color='r', linewidth=0.1, linestyle="-")
plt.plot(time2[1:-14], By[1:-14], label='Canal 3', color='b', linewidth=0.1, linestyle="-")
plt.legend(loc='upper right', frameon=False, )

# Create a new subplot for FFT
plt.subplot(212)
plt.title('Fast Fourier Transform')
plt.ylabel('Power [a.u.]')
plt.xlabel('Frequency Hz')
xaxis2 = np.arange(0,470,10)
plt.xticks(xaxis2)
fft1 = (Bx[51:-14])
fft2 = (By[1:-14])
plt.grid()

# Loop for FFT data
for dataset in [fft1]:
    dataset = np.asarray(dataset)
    freqs, psd = welch(dataset, fs=266336/300, window='hamming', nperseg=8192)
    plt.semilogy(freqs, psd/dataset.size**0, color='r')

for dataset2 in [fft2]:
    dataset2 = np.asarray(dataset2)
    freqs2, psd2 = welch(dataset2, fs=266336/300, window='hamming', nperseg=8192)
    plt.semilogy(freqs2, psd2/dataset2.size**0, color='b')
plt.show()

正如你所看到的,在某些地方放置input会更好,当我运行代码时,我可以将文件名的名称等写入python而不是创建每个单独的python文件,并在代码中使用指定的信息。顺便说一句。我在我的python中使用Pycharm。

python-2.7 pycharm
1个回答
1
投票

如果您要做的就是摆脱硬编码的路径名,您应该能够使用输入变量格式化您的名称字符串

name = raw_input("Name: ")
measurement = raw_input("Measurement: ")
filename =  "C:/Users/PycharmProjects/{0}/{1}".format(name, measurement)

看看raw_inputstring formatting

© www.soinside.com 2019 - 2024. All rights reserved.