简体   繁体   English

文本文件中的Python选项卡不均匀

[英]Python Tabs Uneven in Text File

I am having troubles with writing a tab delimited text file in Python. 我在用Python编写制表符分隔的文本文件时遇到麻烦。 I have it set to write the item ID, width, depth, etc., but when I look at the data file at the end, even though 2 product ID's are the same length, one will be tabbed correctly, and the other will not. 我已设置它写入商品ID,宽度,深度等,但是当我看最后的数据文件时,即使2个商品ID的长度相同,一个也可以正确制表,而另一个则不会。 This is what it look like in the text file: 这是文本文件中的样子:

Product ID      Tariff Number   Net Depth   Net Height  Net Length  Net width   Net Weight

7TAD012610R0010 3917320010  .61 m       .203 m              .61 m               
7TAD012610R0012 3917320010  .66 m       .229 m              .66 m               
7TAD012610R0013 3917320010  .711 m      .178 m              .711 m              
7TAD012610R0014     3917320010  .711 m      .178 m              .711 m              
7TAD013190R0000     7307193060  .102 m      .152 m              .114 m              
7TAD013180R0000     7307193060  .102 m      .203 m              .127 m              
7TAD013180R0002     7307193060  .114 m      .076 m              .127 m              
7TAD013190R0002     7307193060  .102 m      .152 m              .114 m              
7TAD013190R0004     7307193060  .102 m      .152 m              .127 m              
7TAD013180R0005     7307193060  .262 m      .188 m              .107 m              

As you can see, the first 3 are not tabbed out as much as the others even though they appear to be the same length, and have the same number of characters... Here is part of the code I have written for this: 如您所见,尽管前三个字符看起来一样长,并且具有相同的字符数,但它们的制表符并没有那么多,这是我为此编写的代码的一部分:

for URL in URLList:
    try:
        dataSoup = BeautifulSoup(urllib.request.urlopen(URL).read())
        print("Page", pageNo, "retrieved")



        #Try to find data of that type
        try:
            proID = columnLookup(URL, "Product ID:")
            fh.write(str(proID))
            fh.write("\t")
            if len(PID[pageNo - 1]) < 8:
                fh.write("\t")
        except:
            fh.write("\t\t")
        try:
            fh.write(dataSoup.find(id="CustomsTariffNumber").next_sibling.next_sibling.div.text)
            fh.write("\t")
        except:
            fh.write("\t\t")
        try:
            fh.write(dataSoup.find(id="ProductNetDepth").next_sibling.next_sibling.div.text)
            fh.write("\t")
            if len(dataSoup.find(id="ProductNetDepth").next_sibling.next_sibling.div.text) < 8:
                fh.write("\t")
            print("Net Depth:", dataSoup.find(id="ProductNetDepth").next_sibling.next_sibling.div.text)

        #If none is found, skip over that column: Leave it blank
        except:
            fh.write("\t\t")

If anyone could let me know why it's doing this and how to fix it, that would be great. 如果有人能让我知道它为什么这样做以及如何解决它,那就太好了。 I think it has something to do with the characters it contains, but I am at a loss for ideas to fix it, I've tried adding and removing tabs, trying to find the length of both, and it seems like nothing works. 我认为它与其中包含的字符有关,但是我对解决它的想法不知所措,我曾尝试添加和删除制表符,试图找到两者的长度,但似乎没有任何效果。

If it is the right problem, would this be the proper way to strip it?: 如果是正确的问题,这是否是去除它的正确方法?:

        try:
            proID = columnLookup(URL, "Product ID:")
            fh.write(str(proID).strip())
            fh.write("\t")
            if len(PID[pageNo - 1]) < 8:
                fh.write("\t")
        except:
            fh.write("\t\t")

I have found a better way to do this... I have decided to just go with the built-in csv file reader/writer as suggested by hiro protagonist. 我已经找到了一种更好的方法...我决定按照hiro主角的建议使用内置的csv文件读取器/写入器。 This is how I did it if anyone would like to know: 如果有人想知道,我就是这样做的:

        with open('data.csv', 'a') as dataFile:
            csvWriter = csv.writer(dataFile, delimiter=',')
            csvWriter.writerow(productData)

I then have the data values stored in productData! 然后,我将数据值存储在productData中!

Thanks for the help everyone! 感谢大家的帮助!

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

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