Want to load files continuously using the for statement

Asked 1 years ago, Updated 1 years ago, 206 views

I am reading a text file using read_text_file=pd.read_csv(), but I have hundreds of files, so I am thinking about automating them by renaming the text file in the sentence using the for statement continuously.

Therefore, as shown in the code below, I changed the variable i continuously from 1 to 3 in for i in range(1,4): and used name="No"+str(i)+".txt" in the next line, but the following error occurred:

I would appreciate it if you could tell me how to resolve this case.

error messages:

TypeError: unsupported operand type(s) for -: 'str' and 'int'

source code:

 pip install plotly==5.10.0# Installing the library for graph drawing

import pandas aspd
import plotly
import plotly.graph_objs as go
import numpy as np
from plotly.subplots import make_subplots

for i in range (1,4):
  name = "No" + str(i) + ".txt"
  read_text_file=pd.read_csv(name)#Read text file
  name1 = "No" + str(i) + ".csv"
  read_text_file.to_csv(name1,index=None)# Convert text file to csv

  Replace df=pd.read_csv(name1, header=None, sep="\s+")#csv with DataFrame, in this case, csv does not have a header, so specify None

  labels=["TIME", "CH1", "CH2", "CH3", "CH4"]# Make a list of column names for your dictionary
  labels_dict = {num:label for num, label in enumerate(labels)}# csv header is missing, so create a dictionary from the list
  df=df.rename(columns=labels_dict)# Rename the column for DataFrame
  name2 = "No" + str(i) + "_comp.csv"
  # Save the data frame with the column name added as a csv file.
  df.to_csv(name2,index=False)

  df = pd.read_csv(name2)# Load csv with column name added
  name3 = "No" + str(i) + "_comptest.csv"
  new_file2="name3"#Defined to output alias csv later

  df["THL"]=((((df["CH1"]-988)+(df["CH3"]-988)+(df["CH2"]-988)+(df["CH4"]-988)/4)/820)*100
  # df["THL"] = ((((df["CH1"]-1252)+(df["CH3"]-1252)+(df["CH2"]-1252)+(df["CH4"]-1252)/4)/556)*100 #Create a new column with numbers from 1 to 4 columns of existing csv
  df["RUD"]=((((df["CH2"]-988)+(df["CH4"]-988)-((df["CH1"]-988)+(df["CH3"]-988)))/2)/1544)*100 
  df["ELE"]=((((df["CH2"]-988)+(df["CH1"]-988)-((df["CH4"]-988)+(df["CH3"]-988)))/2)/1544)*100
  df["AIL"]=((((df["CH2"]-988)+(df["CH3"]-988)-((df["CH1"]-988)+(df["CH4"]-988)))/2)/1544)*100

  df.to_csv(new_file2,index=False)# Output csv with new column added as another name and set index to False to prevent overwriting

  df=pd.read_csv(name3,index_col=0)#Check Later
  data = [
          go.Scatter(x=df.index, y=df['THL'], name='THL', line=dict(color="#ea553a")), # Specify the DataFrame and Name to Reference Chart in Graph
          ]

  name4 = "Flight Test No" + str(i) + "Transmitter Operation Displacement (20221027)"
  name5 = "Try operation No" + str(i) + ".html"
  layout = go.Layout(#Configuring Graph Layout)
                      title=name4,
                      xaxis = {'title': 'Time [ms]'},
                      yaxis = {'title': 'Operation [%]'},
                      font = {'size':18},
                      width = 1000,
                      height = 600
                      )         

  config=go.Figure(data=data, layout=layout)

  config.update_layout(plot_bgcolor="white")
  config.update_xaxes (linecolor='black', gridcolor='white', mirror=True)
  config.update_yaxes (linecolor='black', gridcolor='white', mirror=True)
  config.update_yaxes (range=[0,100])  

  config.write_html(name5)#Name the file for the output graph
  plotly.offline.plot(fig)

python pandas

2022-11-06 16:04

1 Answers

When considering the proper input data, there are no errors in the following two corrections.

#df=pd.read_csv(name1, header=None, sep=r"\s+")
  df=pd.read_csv(name1,header=None)
#new_file2="name3"
  new_file2 = name3

Also, only the title and axis of the graph were displayed (in the browser), but it is difficult to investigate the cause because I have no experience with plotly.

Also, I can rewrite the details as follows.

#labels_dict={num:label for num, label in enumerate(labels)}
  labels_dict=dict(enumerate(labels))


2022-11-07 05:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.