我有一个.csv(export.csv),其中包含将近9k的行,结构如下:
|---------------------|------------------|---------------|
| Oggetto | valueID | note |
|---------------------|------------------|---------------|
| 1 | work1 |DescrizioneA |
|---------------------|------------------|---------------|
| 2 | work2 |DescrizioneB |
|---------------------|------------------|---------------|
| 3 | work3 |DescrizioneC |
|---------------------|------------------|---------------|
我将在单独的.txt文件中从“注释”列中导出行,然后将该文件命名为“ valueID”列中的值,即work1.txt(work1.txt文件“ DescrizioneA”的内容) 。 从我尝试过的this类似问题开始,失败了,就像这样:
import csv
with open('export.csv', 'r') as file:
data = file.read().split('\n')
for row in range(1, len(data)):
third_col= data
with open('t' + '.txt', 'w') as output:
output.write(third_col[2])
然后我尝试了熊猫
import pandas as pd
data = pd.read_csv("export.csv", engine ='python')
d = data
file = 'file{}.txt'
n = 0 # to number the files
for row in d.iterrows():
with open(file.format(n), 'w') as f:
f.write(str(row))
n += 1
我得到了一些东西,但是:
有什么主意吗?
谢谢
答案 0 :(得分:0)
您可以尝试熊猫:
df=pd.read_csv("export.csv",sep=",")
for index in range(len(df)):
with open(df["valueID"][index] + '.txt', 'w') as output:
output.write(df["note"][index])
答案 1 :(得分:0)
如果您不想使用熊猫,可以这样做
with open('export.csv', 'r') as file:
data = file.read().split('\n')
好的,这是一个开始的一行一行地存储数据的变量
现在,您需要查找每一行的数据。如果数据按照您所说的存储(单个单词或数字,并用空格键隔开),则可以再次拆分它:
text_for_file = ""
for row in range(1, len(data)):
splitted_text = row.split(' ')
text_for_file = '\n'.join([text_for_file, splitted_text[2]])
#So now all you notes are stored in text_for_file string line by line
#write all you data to file
with open("your_file.txt", 'w') as f:
f.write(text_for_file)