简体   繁体   English

Python3:读取用引号分隔的CSV并将其保存在字符串列表中

[英]Python3: Read quote separated CSV and save it in a list of strings

I have a quote-separated CSV with HTML code in it. 我有一个用HTML代码隔开的用逗号分隔的CSV。 I would like to save the content of the cells in a list of strings. 我想将单元格的内容保存在字符串列表中。 In the past I used this code to read from a CSV-file and save it in a list, but now it returns "IndexError: list index out of range" in the last line. 过去,我使用此代码从CSV文件读取并将其保存在列表中,但是现在它在最后一行返回“ IndexError:列表索引超出范围”。 I don't know what to do now. 我现在不知道该怎么办。 Should I rather try to convert the CSV to an other format or can I just rewrite some of the code? 我应该尝试将CS​​V转换为其他格式还是只重写一些代码?

with open("tobetranslated.csv", "r", newline="", encoding="cp1252") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter='"', quotechar='|')
    items = [row[0] for row in csv_reader]

Here are two entries of tobetranslated.csv: 这是tobetranslated.csv的两个条目:

"<div class=""info-holder"" id=""product_bullets_section"">
<p>
VM-2N ist ein Hochleistungs-Verteilverstärker für Composite- oder SDI-Videosignale und unsymmetrisches Stereo-Audio. Das Eingangssignal wird entkoppelt und isoliert, anschließend wird das Signal an zwei identische Ausgänge verteilt.
<span class=""hidden visible-sm-block visible-md-block visible-xs-block visible-lg-block"" id=""decora_msg_container"">
<span style=""font-size: small; font-style: italic;"">* DECORA® is a registered trademark of Leviton Manufacturing Co., Inc</span>
</span>
</p>
<ul>
<li>
<span>Hohe Bandbreite - 400 MHz (-3 dB).</span>
</li>
<li>
<span>Desktop-Grösse - Kompakte Bauform, zwei Geräte können mithilfe des optionalen Rackadapters RK-1 in einem 19 Zoll Rack auf 1 HE nebeneinander montiert werden.</span>
</li>
</ul>
</div>"
"<div class=""info-holder"" id=""product_bullets_section"">
<p>
VM-8H ist ein Verteilverstärker für HDMI-Signale. Er taktet das Eingangssignal neu, entzerrt es und verteilt es an acht identische Ausgänge.
<span class=""hidden visible-sm-block visible-md-block visible-xs-block visible-lg-block"" id=""decora_msg_container"">
<span style=""font-size: small; font-style: italic;"">* DECORA® is a registered trademark of Leviton Manufacturing Co., Inc</span>
</span>
</p>
<ul>
<li>
<span>Max. Datenrate - 6,75 GBit/s (2,25 GBit/s je Grafikkanal).</span>
</li>
<li>
<span>HDTV-kompatibel.</span>
</li>
<li>
<span>HDCP-konform.</span>
</li>
<li>
<span>HDMI-Unterstützung - Deep Color, x.v.Color™, Lip Sync, CEC.</span>
</li>
<li>
<span>3D-Durchleitung.</span>
</li>
<li>
<span>Kramers intelligente EDID-Bearbeitung I-EDIDPro™ - Intelligente EDID Bearbeitungs-Algorithmen sichern eine einfache Plug and Play Installation in HDMI-Anwendungen.</span>
</li>
<li>
<span>Kramers Reclocking - und Entzerrungs Technologie - Baut das Signal neu auf für längere Kabelstrecken.</span>
</li>
<li>
<span>LED-Anzeige eines aktiven Ausgangs.</span>
</li>
<li>
<span>Universal-Netzteil - 100-240 VAC.</span>
</li>
<li>
<span>Standard 19 Zoll Rackeinbau - 1 HE, Rackmontage-Winkel beiliegend.</span>
</li>
</ul>
</div>"

I think you can use the list function to do what you need. 我认为您可以使用列表功能执行所需的操作。

with open("tobetranslated.csv", "r", newline="", encoding="cp1252") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter='"', quotechar='|')

    items = list(csv_reader)

print(items)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM