Python用什么方法可以将换行符分割成多行?

Python054

Python用什么方法可以将换行符分割成多行?,第1张

import pandas as pd

import xlsxwriter

if __name__ == "__main__":

  file_name = r'C:\Users\Administrator\Desktop\test.xlsx'

  """   读取,切割成数组   """

  context = pd.read_excel(file_name)

  num = context['序号'][0]

  value = context['名称'][0]

  new_num = num.split('\n')

  new_value = value.split('\n')

  """   写入数据   """

  workbook = xlsxwriter.Workbook(file_name)

  # 创建工作表

  worksheet = workbook.add_worksheet('sheet1')

 

  # 写单元格

  worksheet.write(0, 0, '序号')

  worksheet.write(0, 1, '名称')

 

  # 写列,其中列需要大写

  worksheet.write_column('A2', new_num)

  worksheet.write_column('B2', new_value)

 

  # 关闭工作簿

  workbook.close()

###顺便给一下你学习的链接吧,这个其实就是个简单的读写文件的应用而已,网页链接

第一个问题,多行字符串用三个引号,当然在非赋值的情况下三个引号表示是注释。。 第二个问题,你可以按行将字符串分割然后输出。 str = """FirstSecondThird"""a = str.split("\n")for i in xrange(len(a)): print a[i] + ": " + str(i)

def splitfile(filename,sizelimit,forline=True):

size=0

i=1

out=open("%s.%04d"%(filename,i),'w')

for line in open(filename):

size=size+1 if forline else size+len(line)

if(size>sizelimit):

size=1 if forline else len(line)

out.close()

i+=1

out=open("%s.%04d"%(filename,i),'w')

out.write(line)

out.close()

if __name__=='__main__':

filename=raw_input("请输入要分隔的文件名:")

forline=raw_input("输入数字0按行分隔,输入其它按大小分隔(请输入:)")

forline=(int(forline)==0)

sizelimit=int(raw_input("请输入分割文件的大小:"))

splitfile(filename,sizelimit,forline)