Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python读取excel文件并从excel中保存N个带有标题和内容的txt文件_Python_Loops_File - Fatal编程技术网

python读取excel文件并从excel中保存N个带有标题和内容的txt文件

python读取excel文件并从excel中保存N个带有标题和内容的txt文件,python,loops,file,Python,Loops,File,我有3列excel文件: index | name | surname 0 | John | White 2 | Bill | Black 3 | Jack | Red 我需要创建N个txt文件(基于行数),标题为列名,内容为列名 例如,基于上述示例,我希望有3个文件,John.txt(内容为“白色”)、Bill.txt(内容为黑色)和Jack.txt(内容为红色)使用pylightxl,您可以非常轻松地执行此操作,请参见 可以使

我有3列excel文件:

 index  |   name  | surname
 0      |   John  | White
 2      |   Bill  | Black
 3      |   Jack  | Red
我需要创建N个txt文件(基于行数),标题为列名,内容为列名


例如,基于上述示例,我希望有3个文件,John.txt(内容为“白色”)、Bill.txt(内容为黑色)和Jack.txt(内容为红色)

使用pylightxl,您可以非常轻松地执行此操作,请参见


可以使用pandas并将值提取为列表来完成此操作

# import and read
import pandas as pd
df = pd.read_excel("your_file.xlsx")

# create lists
names = df["name"].values
file_contents = df["surname"].values

# iterate through lists
for name, content in zip(names, file_contents): 
    f = open(f"{name}.txt", "w")
    f.write(content)

你能发布你试图解决这个问题的代码吗?
# import and read
import pandas as pd
df = pd.read_excel("your_file.xlsx")

# create lists
names = df["name"].values
file_contents = df["surname"].values

# iterate through lists
for name, content in zip(names, file_contents): 
    f = open(f"{name}.txt", "w")
    f.write(content)