创建函数将列表中的索引替换为索引,然后将这些值保存到HTML文件中。 [英] Create Function to replace index in string with index in list, then save those values into an HTML file.

查看:125
本文介绍了创建函数将列表中的索引替换为索引,然后将这些值保存到HTML文件中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,基本上我有一个csv文件具有不同的值。我希望csv的每一行都需要创建一个新的html文件。 csv行中的每个值都需要替换html中的值1 - 7。我试着创建一个函数来处理这个,但我不能让它改变HTML中的值。我可以手动更改该值,但我真的想知道如何使用函数来完成此操作。这不仅会缩短编码的数量,还会使其更加干净和高效。

 输入字符串
输入csv

#函数


#打开southpark csv文件
def opensouthparkFile(openFile1):
southparklist = []
for openFile1:
i.strip()
l = i.split(,)
southparklist.append(l)
return southparklist



useinput = raw_input(Enter the name of打开文件(Marsh,w)
openFile3 =打开(useinput,rU))打开(Broflovski,w)
openFile4 = open(Cartman,w)
openFile5 = open(McCormick,w)
openFile6 = Scotch,w)


southfile = opensouthparkFile(openFile1)



html =
< html>
< P CLASS =western,ALIGN = CENTER STYLE =margin-top:0.08in; margin-bottom:0.25in>
< FONT SIZE = 7 STYLE =font-size:60pt> VALUE1< / FONT>< / P>
< P CLASS =西方ALIGN = CENTER STYLE =margin-top:0.08in; margin-bottom:0.25in>
< FONT SIZE = 7 STYLE =fontSsize:36pt> VALUE2< / FONT>< / P>
< P CLASS = ALIGN = CENTER STYLE =margin-top:0.08in; margin-bottom:0.25in>
< FONT SIZE = 7 STYLE =font-size:36pt> VALUE3< / FONT>< / P>
< P CLASS =西方ALIGN = CENTER STYLE =margin-top:0.08in; margin-bottom:0.25in>
< FONT SIZE = 6 STYLE =font-size:28pt> VALUE4< / FONT>< / P>
< P CLASS =西方ALIGN = CENTER STYLE =margin-top:0.08in; margin-bottom:0.25in>
< FONT SIZE = 6 STYLE =font-size:28pt> VALUE5< / FONT>< / P>
< P CLASS =西方ALIGN = CENTER STYLE =margin-top:0.08in; margin-bottom:0.25in>
< FONT SIZE = 6 STYLE =font-size:28pt> VALUE6< / FONT>< / P>
< P CLASS =西方ALIGN = CENTER STYLE =margin-top:0.08in; margin-bottom:0.25in>
< FONT SIZE = 6 STYLE =font-size:28pt> VALUE7< / FONT>< / p>
< / html>




#使用southpark值替换html文件的功能

def replacehtml(html,somelist):
html = html.replace(VALUE1,somelist [0])
html = html.replace(VALUE2,somelist [1])$ ​​b $ b html = html.replace(VALUE3,somelist [2])
print somelist [1]




replacehtml(html,southfile [0])
replacehtml(html, southfile [1])$ ​​b




$ b openFile2.write(html)

openFile2.close()


解决方案

Python通过一个他们称之为Call-按对象。当您在replacehtml函数中重新分配字符串时,这不会更改原始html字符串,因为字符串是不可变的类型。



最快的解决方法是将字符串更改为

  def replacehtml(html,somelist):
html = html.replace(VALUE1 ,somelist [0])
html = html.replace(VALUE2,somelist [1])$ ​​b $ b html = html.replace(VALUE3,somelist [2])
print somelist [1]
返回html

html = replacehtml(html,southfile [0])


Ok so basically I have a csv file with different values. I want each line from the csv needs to create a new html file. Each value in the line of the csv needs to replace the values1 - 7 in the html. I've tried to create a function to handle this, but I can't get it to change the values in the html. I can change the value manually, but I really want to know how to do it with a function. This would not only shorten the amount of coding, but make it more clean and efficient as well.

import string
import csv

#functions


#open the southpark csv file
def opensouthparkFile(openFile1):
    southparklist = []
    for i in openFile1:
        i.strip()
       l = i.split(",")
       southparklist.append(l)
    return southparklist



useinput = raw_input("Enter the name of the file that you would like to open:")
openFile1 = (open(useinput, "rU"))
openFile2 = open("Marsh", "w")
openFile3 = open("Broflovski", "w")
openFile4 = open("Cartman", "w")
openFile5 = open("McCormick", "w")
openFile6 = open("Scotch", "w")


southfile = opensouthparkFile(openFile1)



html = """
<html>
<P CLASS="western", ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=7 STYLE="font-size: 60pt">VALUE1</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=7 STYLE="fontSsize: 36pt">VALUE2</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=7 STYLE="font-size: 36pt"> VALUE3</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=6 STYLE="font-size: 28pt"> VALUE4</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=6 STYLE="font-size: 28pt"> VALUE5</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=6 STYLE="font-size: 28pt"> VALUE6</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=6 STYLE="font-size: 28pt"> VALUE7</FONT></P>
</html>
"""



#Function for replacing html files with southpark values

def replacehtml(html, somelist):
    html = html.replace("VALUE1", somelist[0])
    html = html.replace("VALUE2", somelist[1])
    html = html.replace("VALUE3", somelist[2])
    print somelist[1]




replacehtml(html, southfile[0])
replacehtml(html, southfile[1])





openFile2.write(html)

openFile2.close()

解决方案

Python passes parameters by a scheme they refer to as "Call-By-Object." When you reassign the string in your replacehtml function, this doesn't change the original html string because strings are an immutable type.

Fastest fix is probably to change the string to a return of the function.

def replacehtml(html, somelist):
    html = html.replace("VALUE1", somelist[0])
    html = html.replace("VALUE2", somelist[1])
    html = html.replace("VALUE3", somelist[2])
    print somelist[1]
    return html

html = replacehtml(html, southfile[0])

这篇关于创建函数将列表中的索引替换为索引,然后将这些值保存到HTML文件中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆