我正在打开一个名为Remarks_Drug.csv
的csv,其中包含产品名称并已映射
连续列中的文件名。我正在对产品栏进行一些操作
删除+
字符后的所有字符串内容。从+
剥离字符串后
字符,我将结果存储在名为product_patterns
的变量中。
现在,我要打开一个新的csv
,我想将for循环的输出写入
两列,第一列包含product_patterns
,第二列包含
相应的filenames
。
我现在得到的输出只是我要寻找的output csv
的最后一行。
我认为我没有正确地循环,因此product_patterns
和文件名的每一行都会
附加在output csv
文件中。
有人可以帮我吗?
在下面附加代码:
import csv
with open('Remarks_Drug.csv', newline='', encoding ='utf-8') as myFile:
reader = csv.reader(myFile)
for row in reader:
product = row[0].lower()
#print('K---'+ product)
filename = row[1]
product_patterns = ', '.join([i.split("+")[0].strip() for i in product.split(",")])
#print(product_patterns, filename)
with open ('drug_output100.csv', 'a') as csvfile:
fieldnames = ['product_patterns', 'filename']
print(fieldnames)
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
print(writer)
#writer.writeheader()
writer.writerow({'product_patterns':product_patterns, 'filename':filename})
样本输入:
Film-coated tablet + TERIFLUNOMIDE, 2011-07-18 - Received approval letter_EN.txt
Film-coated tablet + VANDETANIB, 2013-12-14 RECD Eudralink_Caprelsa II-28 - RSI - 14.12.2017.txt
Solution for injection + MenQuadTT, 395_EU001930-PIP01-16_2016-02-22.txt
Solution for injection + INSULIN GLARGINE, 2017-11-4 Updated PR.txt
Solution for injection + INSULIN GLARGINE + LIXISENATIDE, 2017 12 12 Email Approval Texts - SA1006-.txt
答案 0 :(得分:2)
我希望这是适合您的方法,如果不是,请告诉我,我们进行检查。
@override
void initState() {
final MainModel model = ScopedModel.of(context);
model.bootUp().then(
(value) => Future.delayed(
Duration(seconds: 1, milliseconds: 500),
() {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
value ? HomePage() : FirstScreen()));
},
),
);
super.initState();
}
您还可以使用import csv
with open('Remarks_Drug.csv') as myFile:
reader = csv.reader(myFile)
products_list = list()
filenames_list = list()
for row in reader:
products_list.append(row[0].lower().split("+")[0].strip())
filenames_list.append(row[1])
for index, product in enumerate(products_list):
with open ('drug_output100.csv', 'a') as csvfile:
fieldnames = ['product_patterns', 'filename']
print(fieldnames)
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
print(writer)
writer.writerow({'product_patterns':product, 'filename':filenames_list[index]})
更快,更智能地制作csv文件。
这是熊猫解决方案:
pandas
答案 1 :(得分:2)
<?php
include("mysql/connector.php");
$ergebnis = mysqli_query($db, "SELECT * FROM sm21 ORDER BY id DESC LIMIT 1");
date_default_timezone_set('Europe/Berlin');
$heute = date("j. n. Y");
echo $heute;
echo "<br>";
while($row = mysqli_fetch_object($ergebnis))
{
$datetime_written = $row->date;
echo $datetime_written;
}
?>
这利用了import csv
import pandas as pd
with open('Remarks_Drug.csv', newline='', encoding ='utf-8') as myFile:
reader = csv.reader(myFile)
mydrug = []
for row in reader:
product = row[0].lower()
#print('K---'+ product)
filename = row[1]
product_patterns = ', '.join([i.split("+")[0].strip() for i in product.split(",")])
mydrug.append([product_patterns, filename])
# print(mydrug)
df = pd.DataFrame(mydrug, columns=['product_patterns', 'filename'])
print(df)
df.to_csv('drug_output100.csv', sep=',', index=False)
库。如果您要使用pandas
处理大型csv
文件,则在性能和内存方面将非常方便且高效。这只是上述的替代解决方案。