简体   繁体   中英

How to append in different columns with running for loop in Python3?

I am trying to create a program to collect data from text files into one text file. I am having lots of issues for some reason. One final output file is composed of 10 different starter files (Serial 1-10.txt). This data needs to be placed in a file (text or csv) in different columns before moving on to the next Data folder holding ten more Serial files.

The following code outputs one long column in each final text file, and not 11 different columns (counting the "numbering" column).

for z in datarange:
    f = open('/home/pi/Desktop/Newdata/Data' + str(z) + '.csv', 'a')
   # print(f)
    therange = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    thelist = (folder_list + "Data" + str(z))

    for i in therange:
        lines = open(thelist + "/Serial" + str(i) + ".txt", 'r+')#, encoding='utf-8')  #open that same text file (reading priviledges
        thedata = lines.readline()
        mylist = [int (x) for x in thedata.split(',') if x.strip().isdigit()]
        #locals()["text" + str(i)] = pd.DataFrame(mylist)
        df = pd.DataFrame(mylist)
        f.write(str(df))

I have also tried something like this: (to get the same result. one long column.

df = { 'Column1': [(text1)], 'Column2': [(text2)], 
     'Column3': [(text4)],'Column5': [(text5)],
     'Column6': [(text6)], 'Column7': [(text7)], 'Column8': [(text8)],
     'Column9': [(text9)], 'Column10': [(text10)]}

f.write(str(df))

The data output summary:

Column1
0     145
1     146
2     123
3     154
.
.
.
287   140

Column2
0     145
1     144
.
.
.
287   128

Column3

and so on....

Any advice or things to try?

Just in case anyone has a similar issue, here is what I ended up using. This worked for my application. I want to post it because it fit for an answer for my question to ultimately get what I wanted it to do.

Part of code for question: for z in datarange: f = open('/home/pi/Desktop/Newdata/Data' + str(z) + '.txt', 'a')

    therange = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    thelist = (folder_list + "Data" + str(z))

    for i in therange:
        lines = open(thelist + "/Serial" + str(i) + ".txt", 'r+')#, encoding='utf-8')  #open that same text file (reading priviledges
        thedata = lines.readline()
        mylist = [int (x) for x in thedata.split(',') if x.strip().isdigit()]
        locals()["text" + str(i)] = mylist
        if i == 10:
            try:

               df = pd.DataFrame({ 'Column1': text1, 'Column2': text2, 'Column3': text4, 'Column5': text5,
          'Column6': text6, 'Column7': text7, 'Column8': text8,
         'Column9': text9, 'Column10': text10})
               pd.set_option('max_rows', 288)
               pd.set_option('max_colwidth', 11)


               data = df.to_string(index=False)
               f.write(str(data))
            except:
               os.remove('/home/pi/Desktop/Newdata/Data' + str(z) + '.txt')
               pass

What I was trying to accomplish (Averaging the rows), worked with this set-up. I then was able to sum and divide each line of the created text files, also looping involved to reach each one. **If placed in a csv file, the "Column1 Column2... and data below" was all in ONE COLUMN" in the csv output. So, I decided to mess with a text file instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM