cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Python, Read CSV file

Python, Read CSV file

Usalas
Long-established Member

Trying to open and read a csv file from Python. Program works fine when run locally. However, when run from controller (execute python file via PLC) looks like the path for the csv file is wrong.  Any suggestions? I also tried absolute path and also added activeconfiguration to the relative path. no luck.

7 REPLIES 7

CodeShepherd
Community Moderator
Community Moderator

EDIT: For the general path on the control:

Could you please have a look to this topic.

aFouraker
Occasional Contributor

@Usalas, Is this from a snap of the IDE? 

Usalas
Long-established Member

It is from the IDE itself. Both files, py and csv are in the active configuration

CodeShepherd
Community Moderator
Community Moderator

How does your PLC code look like? Please have a look to this thread to see how to run python script from PLC.

Usalas
Long-established Member

The PLC side is fine. I can execute any python file. The issue is when the python file tries to read or write another file. in this case csv file. 

CodeShepherd
Community Moderator
Community Moderator

When using the play button you mentioned the script is only run locally in the IDE. So your path is not known by the script instance when run on the ctrlX CORE.

You can also start the script in an interpreter instance by using the debugger (e.g. for testing before using the PLC for starting). See my working example for reading and writing:

CodeShepherd_6-1633358981637.png

 

FileName1 = "/var/snap/rexroth-solutions/common/solutions/activeConfiguration/tiger/projects/CsvReadWrite/csvdata1.csv"
FileName2 = "/var/snap/rexroth-solutions/common/solutions/activeConfiguration/tiger/projects/CsvReadWrite/csvdata2.csv"

file1 = open(FileName1,"r")
file2 = open(FileName2,"w")
for x in file1:
    data = x.split(",")
    for y in data:
        print(y)
        file2.write(y + ",")

file1.close()
file2.close()

 

IDE textual editor script launch settingIDE textual editor script launch setting

Usalas
Long-established Member

Thanks.

it was the file path that was wrong. 

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist