简体   繁体   中英

how to split a very long row data into two column by python (from txt file)

I have a txt file which has a very long row of datat (eg as below)

234.45  234.3455 667.4556 566.3311 332.333 564.322 554.2564 21.203 

I want to read that .txt file with python and want to split the data into two column as below:

234.45  234.3455
667.4556 566.3311
332.333 564.322
554.2564 21.203

I am new to python and I am not sure how to load the read .txt file and split the data into column? Anyone can help me? Thanks ahead.

Lets say you have a file called 'sample.txt'. You can do

f = open('sample.txt', 'r')

This will open the file, now for reading the contents of the file, you can use method readlines which will return you list of lines or rows whatever you call them. Now, each line is string and lets say this data is separated by whitespace you can use split method on line to generate columns.

So, your code could be something like this

f = open('sample.txt', 'r')
lines = f.readlines()
for line in lines:
    columns = line.split(' ')
    print(columns)

Hope it helps!

Opens the file

fp = open('concernedfile.txt', 'r')

Read returns everything in the file as a string.

Split separates each number into list items.

Read more about split at http://www.tutorialspoint.com/python/string_split.htm

before_split = fp.read()
data = before_split.split()

Finding the length of the list and appending data

x = len(data)
col1 = []
col2 = []
for i in range(0,x):
    if (i % 2 == 0):
        col1.append(data[i])
    else:
        col2.append(data[i])

Zip can help you aggregate data structures

combinedcol = zip(col1, col2)

Printing columns as tuples. You can make changes here with ease.

for i in combinedcol:
        print i

Essentially you loop through every item and if the index of the item in the list is even you add to col1 and otherwise add to col2

This will work with items separated by any number of spaces

import re
with open("test.txt") as f:
    inputString = f.read()

#remove all extra spaces so all items separated by only one space
inputString = re.sub(r" +", " ", inputString)

itemsInString = inputString.split(" ")

col1 = []
col2 = []
for index, item in enumerate(itemsInString):
    if index % 2 == 0:
        col1.append(float(item))
    else:
        col2.append(float(item))

print(col1)
print(col2)

This gives you the following:
col1 = [234.45, 667.4556, 332.333, 554.2564]
col2 = [234.3455, 566.3311, 564.322, 21.203]

Writing it back out to a file:

writeString = ""
for item in zip(col1, col2):
    writeString += str(item[0]) + " " + str(item[1]) + "\n"

with open("outfile.txt", "w") as f:
    f.write(writeString)


And here's a more optimized way to write to file without string concatenation but it's less clear

with open("outfile.txt", "w") as f:
    f.write("\n".join(([" ".join([str(a[0]), str(a[1])]) for a in zip(col1, col2)])))

Read file :

fp = open("abc.txt")
content = fp.read();
lines = content.split("\t") #split row by tab spaces to form a list

Split the input row to two lists :

l1=[]  #list1 to store col 1
l2=[]  #list2 to store col 2
for i in range(0,len(lines)):
    if(i%2 == 0):
        l2.append(lines[i])
    else:
        l1.append(lines[i])

Zip the list to form groups(which you may later write to files):

for x in list(zip(l1,l2)):
    print(x)

Write to file :

fp=open("E:/efg.txt",'a')
for x in list(zip(l1,l2)):
    fp.write(('\t'.join(x)))
    fp.write('\n')

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