How to transpose lines to column for only 7 rows at a time in file

蹲街弑〆低调 提交于 2019-12-06 07:41:24

Think about what you are trying to accompolish, and how simple it really is.

You have a giant list of things split into 7 lines a piece

first and foremost i would turn everything into a giant list just like you already did

data = input.readlines()

count them

totalUsers = len(data)/7 # it SHOULD be divisible by 7

this gives you how many iterations you should need to go over everything.. now its time to get slicey

users = []
start = 0 #because we start on 0
end = 6 # and end on 6 ( which is the 7th line )
for number in totalUsers:
    person = totalUsers[start:end]   # slicing, learn about it, its cool stuff
    start += 7       # move start up 7
    end +=7           # move end up 7
    users.append(person)
....
data = input.read()  #read it all in
people = [person.replace("\n","") for person in data.split("ID:")]
data_new = "\nID:".join(people)

output.write(data_new.strip())

first read in your whole file as a big chunk

then split your data on "ID:" so that you have a list

for each item replace newlines with nothing

join your "people" list back together with "\nID:" to get one big block of text

write it back out to your output (and strip it so that you get rid of any extra leading \n's)

Why does your code and input file differ? You have "ID:" vs "User Id:", "Email" vs "User Email:", etc..? Well anyways, you can do like this:

#!/usr/bin/python

# open file
input = open ("C:\Documents\Customer.csv","r")

#write to a new file
output = open("C:\Documents\Customer1.csv","w")

lines = [line.replace('\n',',') for line in input.split('ID:')]
output.write("\nID:".join(lines)[1:])

# Close files
input.close()
output.close()

Or, if you totally want to filter for specific fields in case something else pops in, like this:

#!/usr/bin/python

#import regex module
import re

# open input file
input = open ("C:\Documents\Customer.csv","r")

#open output file
output = open("C:\Documents\Customer1.csv","w")

#create search string
search = re.compile(r"""
                        ID:\s\d+|
                        Name:\s\w+\s\w+|
                        Email:\s\w+\@\w+\.\w+|
                        Company:\s\w+|
                        blah1:\s\w+|
                        blah2:\s\w+|
                        blah3:\s\w+
                        """, re.X)

#write to output joining parts with ',' and adding Newline before IDs
output.write(",".join(search.findall(input.read())).replace(',ID:','\nID:'))

# Close files
input.close()
output.close()

Take a note, in the last example it doesn't have to have 7 fields per person :)

And now with duplicates removed (order is not kept, and complete record is compared):

#!/usr/bin/python

#import regex module
import re

# open input file
input = open ("C:\Documents\Customer.csv","r")

#open output file
output = open("C:\Documents\Customer1.csv","w")

#create search string
search = re.compile(r"""
                        ID:\s\d+|
                        Name:\s\w+\s\w+|
                        Email:\s\w+\@\w+\.\w+|
                        Company:\s\w+|
                        blah1:\s\w+|
                        blah2:\s\w+|
                        blah3:\s\w+
                        """, re.X)

# create data joining parts with ',' and adding Newline before IDs    
data = ",".join(search.findall(input.read())).replace(',ID:','\nID:')

# split data into list 
# removing duplicates out of strings with set() and joining result back
# together for the output

output.write("\n".join(set(data.split('\n'))))

# Close files
input.close()
output.close()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!